I have this two class
public class Category
{
public int Id{ get; set; }
public string Name { get; set; }
}
public class Filter
{
public int Id{ get; set; }
public string Name { get; set; }
}
And I have another Entity like this
public class Menu
{
public int Id{ get; set; }
public string Name { get; set; }
public MenuType MenuType { get; set; }
}
That the MenuType is enum Like this
public enum MenuType
{
Category = 0,
Filter = 1
}
i want to know in class menu how can store Category OR Filter
i mean Menu have relate with one Category or one Filter , now how can I make this relationship?
and one other thing is maybe MenuType will extende and added some other menutype and thats class.
Is not possible to have a column Foreign Key (FK) that can reference two tables.
A option would be:
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public List<Menu> Menus { get; set; }
}
public class Filter
{
public int Id { get; set; }
public string Name { get; set; }
public List<Menu> Menus { get; set; }
}
public class Menu
{
public int Id { get; set; }
public string Name { get; set; }
public MenuType MenuType { get; set; }
public int? CategoryId { get; set; }
public int? FilterId { get; set; }
public Category Category { get; set; }
public Filter Filter { get; set; }
}
Inside your context:
modelBuilder.Entity<Menu>().HasOne(x => x.Category).WithMany(x => x.Menus).HasForeignKey(x => x.CategoryId);
modelBuilder.Entity<Menu>().HasOne(x => x.Filter).WithMany(x => x.Menus).HasForeignKey(x => x.FilterId);
And based on MenuType, you either use Category or Filter object.
Related
I have a class named "ODL"
Which holds values of :
{
public int ODLId { get; set; }
public string ODLName { get; set; }
public int? RefODLId { get; set; }
public int VehicleTypeId { get; set; }
public int MotorTypeId { get; set; }
public int GearboxTypeId { get; set; }
public int DoorId { get; set; }
public int VehicleLengthId { get; set; }
public string ProjectName { get; set; }
}
In another class which its name is "ODLModel" and used as a Model Class.
In this Model class there is a "List ODLs" property.
So now I can raech the ID values of VehicleType, MotorType etc.. in a foreach loop.
#foreach (var odl in Model.ODLs)
{
<tr>
<td>#odl.ODLName</td>
<td>#odl.VehicleLengthId</td>
<td>#odl.MotorTypeId</td>
!!! In here I dont want to show Id values but I would like to reach
the related tables VehicleLength.cs etc and take their "Name" values
and print it in here !!!
.....
</tr>
}
</tbody>
As I said, I'd like to show the "name" values in VehicleType.cs, MotorType.cs etc.
Im using EntityFramework, and all of my classes referenced as ID properties in ODL.cs are related to a table.
public DbSet<VehicleType> VehicleTypes { get; set; }
public DbSet<MotorType> MotorTypes { get; set; }
public DbSet<GearboxType> GearboxTypes { get; set; }
public DbSet<Door> Doors { get; set; }
public DbSet<VehicleLength> VehicleLengths { get; set; }
public DbSet<ODL> ODLs { get; set; }
In order to be able to get properties of related items you have to change your ODL class. Instead of declaring int ID props you need to declare "navigation properties". You can read more about it at official documentation
And your ODL class will look like that:
{
public int ODLId { get; set; }
public string ODLName { get; set; }
public int? RefODLId { get; set; }
public virtual VehicleType VehicleType { get; set; }
public virtual MotorType MotorType { get; set; }
public virtual GearboxType GearboxType{ get; set; }
public virtual Door Door { get; set; }
public virtual VehicleLength VehicleLength { get; set; }
public string ProjectName { get; set; }
}
And you will be able to call it like:
var odl = ... // get ODL entity from DbSet
odl.VehicleType.Name
odl.GearboxType.SomeProperty
An ojective should have one Category(one-to-one),
the objective has Actions and each one of them should have one Action Category(one-to-one) and many Resource Categories(one-to-many).
I don't know how to configure the mapping. Also i'm not sure if my design is correct, how to manage this case properly?
Summary:
I need 3 relationships.
1) one-to-one between Objective and Category on ObjectiveCategoryId property of Objective.
2) one-to-one between ObjectiveAction and Category on ActionCategoryId property of ObjectiveAction.
3) one-to-many between ObjectiveAction and Category on ObjectiveId property of Category.
public class Category {
public int Id { get; set; }
public string Name { get; set; }
}
public class Objective {
private ICollection<DevelopmentObjectiveAction> _actions;
public int Id { get; set; }
public string Name { get; set; }
public int ObjectiveCategoryId { get; set; }
public Category ObjectiveCategory { get; set; }
public ICollection<ObjectiveAction> Actions {
get => _actions ?? (_actions = new List<ObjectiveAction> ());
set => _actions = value;
}
}
public class ObjectiveAction {
private ICollection<Category> _resourceCategories;
public int Id { get; set; }
public string Name { get; set; }
public long ObjectiveId { get; set; }
public long Objective { get; set; }
public int ActionCategoryId { get; set; }
public Category ActionCategory { get; set; }
public ICollection<Category> ResourceCategories {
get => _resourceCategories ?? (_resourceCategories = new List<Category> ());
set => _resourceCategories = value;
}
}
I have 3 class, 2 are simple class and 1 combines the two of them.
public class Person:Base
{
public DateTime DOB { get; set; }
public string first { get; set; }
public string last { get; set; }
}
public class Roles:Base
{
public string Description { get; set; }
}
public class RolePerson:Base
{
public int pID { get; set; }
public virtual Person PersonID { get; set; }
public int rID { get; set; }
public virtual Roles RoleID { get; set; }
public int order { get; set; }
}
Now I have method that would select RolePerson witch would grab all member of pID with the id that i provide in the argument, and return the RoleID field.
this is how my method look like
public IEnumerable<Roles> PersonRoles(int personID)
{
return db.RolePerson.Where<RolePerson>((p) => p.pID == personID);
}
How can i say to return the role and not the roleperson.
Update:
My base class is as follow
public abstract class Base
{
public int ID { get; set; }
public Boolean isValid { get; set; }
public DateTime createdOn { get; set; }
public int createdID { get; set; }
public Person createdPerson { get; set; }
public DateTime updatedOn { get; set; }
public int updatedID { get; set; }
public Person updatedPerson { get; set; }
}
You can run a select statement in the end.
return d.RolePerson.Where(p => p.PId == personID).Select(r=> r.RoleID);
I've three entities, say Parent, Child & GrandChild. This GrandChild has got three lists(a,b & c).
How to retrive all these lists using "Include" in Entity Framework?
I can retrive one list like this: Include("Parent.Child.GrandChild.a"),
but when I add another Include like : Include("Parent.Child.GrandChild.b"), it throws an error.
How will i retrive all these lists in a single query?
My code is as follows:
objUserNew = objContaxt.ContextRegistration
.Include("listing.postlist.commentslist")
.Include("listing.postlist.taglist")
.Include("listing.postlist.knocklist")
.FirstOrDefault(x => x.id == objUser.id);
public class registration
{
public int id { get; set; }
public string firstname { get; set; }
public forgtPass forgtPass { get; set; }
public List<listing> listing { get; set; }
}
public class listing
{
public int id { get; set; }
public string address { get; set; }
public List<post> postlist { get; set; }
}
public class post
{
public int id { get; set; }
public string imgUrl { get; set; }
public List<tag> taglist { get; set; }
public List<comments> commentslist { get; set; }
public List<knoqs> knoqs { get; set; }
}
public class tag
{
public string name { get; set; }
}
public class comments
{
public int id { get; set; }
public string comment { get; set; }
public status status { get; set; }
}
public class knoqs
{
public int id { get; set; }
public virtual int registration_id { get; set; }
[ForeignKey("registration_id")] public registration registration { get; set; }
public status status { get; set; }
}
I have two models and for which I will create tables through migration and database update. My first model is named Service, and it consists of these fields:
public class Service
{
public int ServiceID { get; set; }
public string ServiceType { get; set; }
public string Category { get; set; }
public string Subcategory { get; set; }
public int Count { get; set; }
}
My second model is called Business, and it has the following fields:
public class Business
{
public int BusinessID { get; set; }
public string BusinessName { get; set; }
public string BusinessWebsite { get; set; }
public string BusinessAddress { get; set; }
public string BusinessCity { get; set; }
public string BusinessState { get; set; }
public string BusinessZip { get; set; }
public string BusinessDescription { get; set; }
[Range(0.0, 5.0)]
public int Rating { get; set; }
public DateTime LastLogIn { get; set; }
// Need to add more fields
}
The point is that I want to add Category and Subcategory fields into my Business model, but the values of Category and Subcategory fields, should be one of the values inside the Service table's values for Category and Subcategory.
Simply, I want to connect those two fields. How can I achieve it? Should I just put a Service property inside the Business model?
Break out a separate entity for "Category" and then use foreign keys:
public class Category
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
public class Service
{
...
public virtual Category Category { get; set; }
public virtual Category Subcategory { get; set; }
}
// Do the same when adding category/subcategory fields to `Business`
If you want to ensure that these categories are only tied to Service (and you potentially have other types of categories or something) you can always just make the entity ServiceCategory or something and only create a relationship to it from Service.
You need to separate your break out your database to store lookup table for category and a lookup table for subcategory.
Then you can create:
public class Category {
public int CategoryId { get; set; }
public string Name { get; set; }
}
public class SubCategory {
public int SubCategoryId { get; set; }
public string Name { get; set; }
}
Then change your service class to:
public class Service
{
public int ServiceID { get; set; }
public string ServiceType { get; set; }
public int CategoryId { get; set; }
public int SubcategoryId { get; set; }
public int Count { get; set; }
}
and change your Business class to:
public class Business
{
public int BusinessID { get; set; }
public string BusinessName { get; set; }
public string BusinessWebsite { get; set; }
public string BusinessAddress { get; set; }
public string BusinessCity { get; set; }
public string BusinessState { get; set; }
public string BusinessZip { get; set; }
public string BusinessDescription { get; set; }
public int CategoryId { get; set; }
public int SubCategoryId { get; set; }
[Range(0.0, 5.0)]
public int Rating { get; set; }
public DateTime LastLogIn { get; set; }
// Need to add more fields
}