Code first one to one select query - c#

I try using Code First but I have some troubles, I think I forget or miss something ...
I have two pretty simple models : (a car have one FuelType, it's a one to one relation)
public class Car
{
public int CarID { get; set; }
public string CarName { get; set; }
public Fuel FuelType { get; set; }
...
}
public class Fuel
{
public int FuelID { get; set; }
public string Name{ get; set; }
...
}
My contextClass :
public class CarDbContext : DbContext
{
public DbSet<Car> Cars { get; set; }
public DbSet<Fuel> Fuels { get; set; }
public CarDbContext()
: base("CarDbConnectionString")
{
Database.SetInitializer<CarDbContext>(new CreateDatabaseIfNotExists<CarDbContext>());
}
And my main program :
class Program
{
static void Main(string[] args)
{
IList<Car> maListe;
using (var ctx = new CarDbContext())
{
var car = from c in ctx.Cars
select c;
maListe = car.ToList<Car>();
}
foreach (Car c in maListe)
Console.WriteLine("Car: {0} {1}", c.CarName, c.FuelType.Name);
Console.ReadLine();
}
}
In my console, I can see CarName but not the FuelType.Name of the car.
I tried few things like changing my query to this :
var car = from c in ctx.Cars.Include("Fuels")
select c;
But I get an exception :
A specified Include path is not valid. The Entity Type 'Car' does not declare a navigation property with the name 'Fuels'
Does someone can explain to me what I did wrong or what I forget ?

You have navigation property named FuelType:
var cars = ctx.Cars.Include("FuelType"); // not `Fuels`

If you want to 'eager-load' FuelType, try:
var car = from c in ctx.Cars.Include("FuelType")
Alternatively, you can mark it as virtual and have FuelType 'lazy-loaded' for you:
public class Car
{
public int CarID { get; set; }
public string CarName { get; set; }
public virtual Fuel FuelType { get; set; }
...
}

Are you sure that it's a one-to-one relation ? a FuelType is only associated with one car ?
Anyway, in order to make a one-to-one relation you have to include a navigation property for the Principal entity in your Dependent entity, as follow:
public class CarDbContext : DbContext
{
public CarDbContext()
: base("CarDbConnectionString")
{
Database.SetInitializer<CarDbContext>(new CreateDatabaseIfNotExists<CarDbContext>());
}
public DbSet<Car> Cars { get; set; }
public DbSet<Fuel> Fuels { get; set; }
}
public class Car
{
[Key]
public int CarID { get; set; }
public string CarName { get; set; }
public virtual Fuel FuelType { get; set; } // set as virtual
}
public class Fuel
{
[Key, ForeignKey("Car")] // primary and foreign key
public int FuelID { get; set; }
public string Name { get; set; }
public virtual Car Car { get; set; } // must include a navigation property for the Principal entity
}
But if it's a one-to-many relationship, you can handle it as follow:
public class Car
{
[Key]
public int CarID { get; set; }
public string CarName { get; set; }
public int FuelTypeID { get; set; }
[ForeignKey("FuelTypeID")]
public virtual Fuel FuelType { get; set; }
}
public class Fuel
{
public int FuelID { get; set; }
public string Name { get; set; }
}
public class CarDbContext : DbContext
{
public CarDbContext()
: base("CarDbConnectionString")
{
Database.SetInitializer<CarDbContext>(new CreateDatabaseIfNotExists<CarDbContext>());
}
public DbSet<Car> Cars { get; set; }
public DbSet<Fuel> Fuels { get; set; }
}

Thanks to all for answering me.
Your answers really make me progress in EF understanding.
By the way I was really stuck with the "System.Reflection.TargetInvocationException" Exception.
I changed so many things in my code than now it works but unfortunately I cannot understand why :(

Related

How do I set up a one-to-many relationship using Entity Framework Core?

I'm working on a trucking API using Entity Framework (EF) Core. Basic CRUD operations are working fine using the repository pattern. There is an error in
configurations I am implementing, however.
I want to obtain multiple trailers and trucks associated with single load, reflecting the one-to-many relationship.
public class LoadConfiguration : IEntityTypeConfiguration<Load>
{
public void Configure(Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder<Load> builder)
{
builder.Property(p=>p.Id).IsRequired();
builder.HasOne(t=>t.Customer).WithMany().HasForeignKey(p=>p.CustomerId);
builder.Property(p=>p.LoadedFrom).IsRequired();
builder.HasMany(p=>p.Trailer).WithOne().HasForeignKey(t=>t.TrailerId);
builder.HasMany(p=>p.Truck).WithOne().HasForeignKey(t=>t.TruckId);
builder.Property(p=>p.Destination).IsRequired();
}
}
public class Truck:BaseEntity
{
public int PlateNo { get; set; }
public string ModelName { get; set; }
public Location StateCode { get; set; }
public int PollutionCertificateValidity { get; set; }
public int DateOfPurchase { get; set; }
public int FitnessCertificateValidity { get; set; }
}
public class Load:BaseEntity
{
public Customer Customer { get; set; }
public int CustomerId { get; set; }
public string LoadedFrom { get; set; }
public Trailer Trailer { get; set; }
public int TrailerId { get; set; }
public Truck Truck { get; set; }
public int TruckId { get; set; }
public string Destination { get; set; }
}
public class Trailer:BaseEntity
{
public int TrailerCapacity { get; set; }
public Truck Truck { get; set; }
public int TruckId { get; set; }
}
public class BaseEntity
{
public int Id { get; set; }
}
A one-to-many relationship is defined by using navigation collections, that has the capacity to hold many Trucks and Trailers. You can choose the collection type freely, but I would suggest ICollection generic type.
Modify your Load class as follows:
public class Load:BaseEntity
{
public Customer Customer { get; set; }
public int CustomerId { get; set; }
public string LoadedFrom { get; set; }
public string Destination { get; set; }
// navigation collections
public ICollection<Trailer> Trailers { get; set; }
public ICollection<Truck> Trucks { get; set; }
}
You will then be able to set up the relationship in your LoadConfiguration class by using
the pluralized name:
builder.HasMany(p=>p.Trailers).WithOne();
builder.HasMany(p=>p.Trucks).WithOne();
.. even though EF Core will be smart enough to figure out the relation by convention so the fluent configuration is redundant.

Entity Framework Code First creates classes from two tables and relationships one to many

I create an application and as an example for testing I take a table of orders. I have questions about class modeling.
I have 3 classes:
public class Car
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
public class Part
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
class Order
{
public Order()
{
Cars = new List<Car>();
Parts = new List<Part>();
}
public int OrderId { get; set; }
public int CarId { get; set; }
public int PartId { get; set; }
public ICollection<Car> Cars { get; set; }
public ICollection<Part> Parts { get; set; }
}
I do not know if this model is ok. What do you think? Because something does not go here: / In the application:
I can not add cars or parts to the order that I do not have in the database.
In the table of orders I would like to see only the order Id, the value of the order, and the Id of the car and Id of the part that was bought.
 
I would like the Car and Part tables to have no data about orders. I would like to only add parts or cars in the application, later only be able to select from them in the order section.
Let's start with the physical tables you will need:
Part { Id, Name, Price }
Car { Id, Name, Price }
Order { Id }
OrderPart* { OrderId, PartId }
OrderCar* { OrderId, CarId }
The last two tables are called "join tables" because you need them to be able to store multiple parts and multiple cars on the same order, but are not really tables you think of as being part of your model.
Entity Framework will automatically make these join tables if you set up your classes as follows:
public class Car
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
public class Part
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
class Order
{
public Order()
{
Cars = new List<Car>();
Parts = new List<Part>();
}
public int OrderId { get; set; }
public virtual ICollection<Car> Cars { get; set; }
public virtual ICollection<Part> Parts { get; set; }
}
Note that the ICollection<> properties on the Car and Part table will be the clue to EF that it needs to make the join table. Also, remember that you need "virtual" on your navigation properties.
It is good model ?
One Pizza may have a few idgredience
One Pizza may have one sauce under the cheese
One Order may have a few idgredience and a few sauces.
It is my classes :
public class Suace
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
public class Pizza
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public ICollection<Idgredient> Idgredients { get; set; }
public Sauce Sauce {get;set;}
public virtual ICollection<Order> Orders { get; set; }
}
class Order
{
public Order()
{
Cars = new List<Car>();
Parts = new List<Part>();
}
public int OrderId { get; set; }
public virtual ICollection<Car> Suace { get; set; }
public virtual ICollection<Part> Pizza { get; set; }
}
public class Idgredient
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Pizza> Pizzas { get; set; }
}

Entity Framework, table with 2 foreign keys to 2 different tables

I have 3 models with fields in it like the following:
public class RootObject
{
[Key]
public int RootObjectId { get; set; }
[ForeignKey("RootObjectId")]
public virtual AObject AObject { get; set; }
[ForeignKey("RootObjectId")]
public virtual BObject BObject { get; set; }
public string Name { get; set; }
}
public class AObject
{
[Key]
public int AObjectId { get; set; }
//Other fields
}
public class BObject
{
[Key]
public int BObjectId { get; set; }
//Other fields
}
I want it so that if I were to visually inspect the RootObject table I would see see a list of RootObjectId's and Name's. For ease, lets assume even numbered RootObjectId's are mapped to AObjectId's and odds are mapped to BObjectId's. If I were to visually inspect AObject, I would expect to see the ID's 2, 4, 6, ... that are FK's for RootObject. If were to visually inspect BObject, I would expect to see the ID's 1, 3, 5, ... that are FK's for RootObject.
Currently, when I try this approach I get the following error:
"An error occurred while updating the entries...Referential integrity constraint violations. A Dependent Role has multiple principals with different values."
I tried to remove the FK attributes in RootObject but that created 2 additional columns in RootObject that were populated with ID numbers. I don't want this since every RootObject has either one AObject or one BObject. It can't have both.
To me, you are looking for something for which the TPT (Table per Type) approach in Entity Framework could be a solution. Applied to your case (there are many approaches, but this I tested and it works):
public class RootObject
{
[Key]
public int RootObjectId { get; set; }
public string Name { get; set; }
}
[Table("AObjects")]
public class AObject : RootObject
{
//Other fields
public string AField { get; set; }
}
[Table("BObjects")]
public class BObject : RootObject
{
//Other fields
public string BField { get; set; }
}
For the DbContext class:
public DbSet<RootObject> RootObjects { get; set; }
public DbSet<AObject> AObjects { get; set; }
public DbSet<BObject> BObjects { get; set; }
Seed example:
AObject a1 = new AObject() { Name = "ImA", AField = "adata" };
BObject b1 = new BObject() { Name = "ImB", BField = "bdata" };
context.AObjects.Add(a1);
context.BObjects.Add(b1);
context.SaveChanges();
I don't think it is possible to use ONE column to be a foreign key to two different tables. You should rather think of two (optional) FK like:
public class RootObject
{
[Key]
public int RootObjectId { get; set; }
public int? EvensAObjectId { get; set; }
public int? OddsBObjectId { get; set; }
[ForeignKey("EvensAObjectId")]
public virtual AObject AObject { get; set; }
[ForeignKey("OddsBObjectId")]
public virtual BObject BObject { get; set; }
public string Name { get; set; }
}
To set up 1-0..1 relationships you need to define the relationships explicitly during model configuration.
public class Model1 : DbContext
{
public Model1()
: base("name=Model1")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<AObject>()
.HasRequired(e => e.RootObject).WithOptional(r => r.AObject);
modelBuilder.Entity<BObject>()
.HasRequired(e => e.RootObject).WithOptional(r => r.BObject);
base.OnModelCreating(modelBuilder);
}
public virtual DbSet<RootObject> RootObjects { get; set; }
public virtual DbSet<AObject> AObjects { get; set; }
public virtual DbSet<BObject> BObjects { get; set; }
}
public class RootObject
{
[Key]
public int RootObjectId { get; set; }
public virtual AObject AObject { get; set; }
public virtual BObject BObject { get; set; }
public string Name { get; set; }
}
public class AObject
{
[Key]
public int AObjectId { get; set; }
public virtual RootObject RootObject { get; set; }
}
public class BObject
{
[Key]
public int BObjectId { get; set; }
public virtual RootObject RootObject { get; set; }
}
You'll want to be really careful when setting RootObject.AObject and RootObject.BObject, as if there is already a related row you will get an error when you save. Also, I don't think there's any way to get EF enforce the constraint that each RootObject must have either an AObject or a BObject, but not both - you'd need to enforce that in your code.

Using Generics with EF classes

I'm using EF6 with Code First and have a few tables with virtually the same schema. I would like to be able to perform queries on these tables and return the results to a common object (class) rather than creating a new one for each.
So for example, EF won't allow:
public class Product1 {
public int id { get; set; }
public string name { get; set; }
}
public DbSet<Product1> Products1 { get; set; }
public DbSet<Product1> Products2 { get; set; }
So I have to define a second POCO:
public class Product1 {
public int id { get; set; }
public string name { get; set; }
}
public class Product2 {
public int id { get; set; }
public string name { get; set; }
}
public DbSet<Product1> Products1 { get; set; }
public DbSet<Product2> Products2 { get; set; }
At least I would like to be able to treat results from these POCOs the same so that I can plug results into another class:
public class SomeClass {
public <Product1 or Product2> Product { get; set; }
}
Be able to store the result from either db table in the same object:
SomeClass someclass = new SomeClass();
someclass.Product = _context.Products1.Where(p => p.id == 1).First();
or
someclass.Product = _context.Products2.Where(p => p.id == 1).First();
int thisId = someclass.Product.id;
How do I make someclass.Product generic so that it will accept either Product1 or Product2?
You would have to make the classes inherit from an interface and then use that interface in a generic type constraint.
public interface IProduct
{
int id { get; set; }
string name { get; set; }
}
public class Product1 : IProduct
{
public int id { get; set; }
public string name { get; set; }
}
public class Product2 : IProduct
{
public int id { get; set; }
public string name { get; set; }
}
Then you could define SomeClass as follows:
public class SomeClass<TProduct> where TProduct : IProduct
{
public TProduct Product { get; set; }
}

Multiple collections of same type in entity framework

I have these two very simple classes.
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public Group Group { get; set; }
}
public class Group
{
public int Id {get;set;}
public ICollection<Person> Teachers { get; set; }
public ICollection<Person> Students { get; set; }
}
I would like EF to keep Teachers seperated from Students however they both get jumbled into a Person table with no way to distinguish between them.
Any ideas?
There are two ways to do it;
first : use a tag or enums in the Person object
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public Group Group { get; set; }
public bool IsFaculty { get; set; }
}
or
public enum PersonType { Teacher, Student };
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public Group Group { get; set; }
public PersonType PropPersonType { get; set; }
}
second : work object oriented with inheritance. This method has my preference because it's easy to manage and expand if you want to expand it.
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public Group Group { get; set; }
}
public class Student : Person
{
public int Year { get; set; }
// other student related fiels.
}
public class Teacher : Person
{
public List<Course> Courses { get; set; }
// other teacher related fields
}
Your Group is then
public class Group
{
public int Id {get;set;}
public ICollection<Teacher> Teachers { get; set; }
public ICollection<Student> Students { get; set; }
}
Separate in two different classes, and then inherit both of them with Person, because all the teachers and students are Persons, but not all Persons are teachers and students.
public class Person
{
}
public class Teacher : Person
{
}
public class Student : Person
{
}
I hope this helps
A little bit different case (when class is holding reference to two identical objects), but can be helpful:
public class Mission
{
//DB objects
public int Id { get; set; }
public int SourceLocationId {get; set}
public int DestinationLocationId {get; set}
//Virtual objects
public virtual Location SourceLocation { get; set; }
public virtual Location DestinationLocation { get; set; }
}
public class Location
{
//DB objects
public int Id {get;set;}
//Virtual objects
public virtual ICollection<Mission> SourceMissions { get; set; }
public virtual ICollection<Mission> DestinationMissions { get; set; }
}
Then all you need to do is bind it properly in OnModelCreating:
modelBuilder.Entity<Mission>()
.HasOptional(m => m.SourceLocation) //Optional or Required
.WithMany(sm => sm.SourceMissions)
.HasForeignKey(to => to.SourceLocationId);
modelBuilder.Entity<Mission>()
.HasOptional(m => m.DestinationLocation) //Optional or Required
.WithMany(sm => sm.DestinationMissions)
.HasForeignKey(to => to.DestinationLocationId);
I think you need some flag to distinguising them (I can't really belive you that can't). And after you can use TPH inheritance approach. See more info here and here.
To have Teachers and Students in two separate tables inherit from an abstract base:
public abstract class Person
{
public int Id { get; set; }
public string Name { get; set; }
public Group Group { get; set; }
}
public class Student : Person
{
public int Year { get; set; }
// other student related fiels.
}
public class Teacher : Person
{
public List<Course> Courses { get; set; }
// other teacher related fields
}
public class Group
{
public int Id {get;set;}
public ICollection<Teacher> Teachers { get; set; }
public ICollection<Student> Students { get; set; }
}

Categories