Here are my models. I have one to one mapping for Vehicle and Driver. I will have the vehicle created first and then map the driver to the vehicle.
public class Driver
{
public int Id { get; set; }
public String Name { get; set; }
public int VehicleId { get; set; }
public virtual Vehicle Vehicle { get; set; }
}
public class Vehicle
{
public int Id { get; set; }
public String Name { get; set; }
public virtual Driver Driver { get; set; }
public int VehicleGroupId { get; set; }
public virtual VehicleGroup Vehicles { get; set; }
}
I want to use VehicleId property in Driver class to keep id of vehicle the driver is driving.
I've written the following Fluent API code:
modelBuilder.Entity<Vehicle>()
.HasRequired(d => d.Driver)
.WithRequiredPrincipal();
But it creates a new column in Drivers table - Vehicle_VehicleId and maps it to the VehicleId on Vehicle table. I want the VehicleId of Driver table to map.
Also, i'm brand new to EF and Fluent API. I find it extremely confusing choosing between WithRequiredDependent and WithRequiredPrincipal. Would be glad if you can describe it in simple words. Thanks.
This line:
public int VehicleId { get; set; }
is telling EF, through code-conventions, that you want a foreign key in Driver pointing to Vehicle.
The following is telling EF that you want a 1:1 relationship from Driver to Vehicle:
public virtual Vehicle Vehicle { get; set; }
You should remove both and stick with your Fluent API configuration.
Regarding WithRequiredPrincipal vs. WithRequiredDependent:
You are specifying a compulsory relationship between Vehicle and Driver, with navigation from Vehicleto Driver, thus: Vehicle 1 --> 1 Driver
(Vehicle is the principal and Driver the dependent, since the navigation property is located in Vehicleand pointing to Driver .)
modelBuilder.Entity<Vehicle>()
.HasRequired(d => d.Driver)
.WithRequiredDependent();
You are specifying a compulsory relationship between Vehicle and Driver, with navigation from Driver to Vehicle, thus: Vehicle 1 <-- 1 Driver
(Vehicle is the dependent and Driver the principal, since the navigation property is located in Driver pointing to Vehicle.)
These two are analogous:
modelBuilder.Entity<Vehicle>()
.HasRequired(v => v.Driver)
.WithRequiredPrincipal();
modelBuilder.Entity<Driver>()
.HasRequired(d => d.Vehicle)
.WithRequiredDependent();
EF creates the Vehicle_VehicleId column because you have VehicleId and Vehicle on your Driver Entity.
Remove VehicleId and Vehicle from your Driver Entity:
public class Driver
{
public int Id { get; set; }
public String Name { get; set; }
}
public class Vehicle
{
public int Id { get; set; }
public String Name { get; set; }
}
Using:
modelBuilder.Entity<Vehicle>()
.HasRequired(d => d.Driver)
.WithRequiredPrincipal();
you are setting the relationship so no need to include manual properties in your entity classes.
You get the VehicleId from the navigation property Vehicle:
IQueryable<int> vehicleIds = context.Drivers.Select(x => x.Id == 123).Vehicles.Id;
Related
I am making a web app similar to google classroom in that you can join classes.
I have a class "Account" and inside that account I have a list that should hold the IDs of all the classes the account has joined. I tried to make the list a list of longs, but I couldn't do that because I got the error:
System.InvalidOperationException: 'The property
'Account._classesJoined' could not be mapped, because it is of type
'List' which is not a supported primitive type or a valid entity
type. Either explicitly map this property, or ignore it using the
'[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in
'OnModelCreating'.
The way I solved this problem is to create a class "JoinedClassId" to make a list of instead, with a property "classIdNumber". However, during testing, I noticed that the JoinedClassIds that I added to the the Account object were not saving. I think this is because I am not saving the database table for the JoinedClassId class.
Do I have to create a database context and controller for the JoinedClassId class? I don't want to be able to manipulate the JoinedClassId class from the API, I'm only using it as a data container. Is there a way I could either create a long list and save it or save the JoinedClassIds?
In EF Core "Many-to-many relationships without an entity class to represent the join table are not yet supported".
Book -> Category has many-to-may rel so this should create the 3 tables in DB :
Books, Category and BookCategory
public class Book
{
public int BookId { get; set; }
public string Title { get; set; }
//public ICollection<Category> Categories { get; set; } // cannot appear
// For the many-to-many rel
public List<BookCategory> BookCategories { get; set; }
}
public class Category
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
//public ICollection<Book> Books { get; set; } // cannot appear
// For the many-to-many rel
public List<BookCategory> BookCategories { get; set; }
}
// Class because of the many-to-many rel
public class BookCategory
{
public int BookId { get; set; }
public Book Book { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
}
public class MyContextDbContext : DbContext
{
public MyContextDbContext(DbContextOptions<MyContextDbContext> dbContextOptions)
: base(dbContextOptions)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<BookCategory>()
.HasKey(t => new { t.BookId, t.CategoryId });
modelBuilder.Entity<BookCategory>()
.HasOne(bctg => bctg.Book)
.WithMany(ctg => ctg.BookCategories)
.HasForeignKey(book => book.CategoryId);
modelBuilder.Entity<BookCategory>()
.HasOne(bctg => bctg.Category)
.WithMany(ctg => ctg.BookCategories)
.HasForeignKey(ctg => ctg.BookId);
}
public DbSet<Book> Book { get; set; }
public DbSet<Category> Category { get; set; }
}
I have read a lot of related questions about this topic but none of them seemed to address my problem, so please bear with me.
I am new to EF and trying to establish the following relationship, in ASP .NET MVC, using EF6:
I need to have two permanent tables, Drivers and Cars. I now need to create a relationship between these tables when a Driver is associated to a Car. But one Driver can only be assigned to one Car.
A Driver may not always be associated to a Car and vice-versa and I want to maintain both tables even if there isn't always an association between them, so that is why I believe I need to have an additional table exclusively to make this connection. Which I think will create a 1:1:1 relationship between these classes.
Below is the model for my POCO classes.
Models
public class Driver
{
public int DriverID { get; set; }
public string Name { get; set; }
//other additional fields
public DriverCar DriverCar { get; set; }
}
public class Car
{
public int CarID { get; set; }
public string Brand { get; set; }
//other additional fields
public DriverCar DriverCar { get; set; }
}
public class DriverCar
{
public int DriverCarID { get; set; }
public int DriverID { get; set; }
public Driver Driver { get; set; }
public int CarID { get; set; }
public Car Car { get; set; }
}
I have tried configuration the relationships using Fluent API but I believe I am doing it completly wrong since I have got errors such as:
Introducing FOREIGN KEY constraint 'FK_dbo.DriverCar_dbo.Car_CarId' on
table 'DriverCar' may cause cycles or multiple cascade paths. Specify
ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN
KEY constraints. Could not create constraint or index. See previous
errors.
Fluent Api
modelBuilder.Entity<DriverCar>()
.HasRequired(a => a.Driver)
.WithOptional(s => s.DriverCar)
.WillCascadeOnDelete(false);
modelBuilder.Entity<DriverCar>()
.HasRequired(a => a.Car)
.WithOptional(s => s.DriverCar)
.WillCascadeOnDelete(false);
I am really not sure if I am missing something or if there is some better approach to handle this situation and I would appreciate so much if someone can give me some feedback on how to solve this.
Update
Just found an interesting answer here: Is it possible to capture a 0..1 to 0..1 relationship in Entity Framework?
Which I believe is exactly what I want: a 0..1 to 0..1 relationship. But all the mentioned options seem too complex and I'm not quite sure which one is the best or how to even correctly implement them.
Are these type of relationships supposed to be so hard to implement in EF?
For example, I tried Option 1 but it created a 0..1 to many relationship from both tables - Driver to Car and Car to Driver. How am I suppose to create an unique association between them then?
Try this for your models. Virtual enables lazy loading and is advised for navigation properties. DataAnnotations showing the Foreign Keys (or use fluent) to be sure each relationship is using the correct key.
public class Driver
{
public int DriverID { get; set; }
public string Name { get; set; }
//other additional fields
public DriverCar? DriverCar { get; set; }
}
public class Car
{
public int CarID { get; set; }
public string Brand { get; set; }
//other additional fields
public DriverCar? DriverCar { get; set; }
}
public class DriverCar
{
public int DriverCarID { get; set; }
[ForeignKey("Driver")]
public int DriverID { get; set; }
public Driver Driver { get; set; }
[ForeignKey("Car")]
public int CarID { get; set; }
public Car Car { get; set; }
}
modelBuilder.Entity<Driver>()
.HasOptional(a => a.DriverCar)
.WithRequired(s => s.Driver)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Car>()
.HasOptional(a => a.DriverCar)
.WithRequired(s => s.Car)
.WillCascadeOnDelete(false);
Note: Changed to Data Annotations for Foreign Keys. Inverted fluent statements. Fixed Driver to Car in second relationship.
Here is a simple way to create a one to zero. Note that I'm a fan of keeping the Id of all tables as just Id, not CarId etc, just my style. This is just a console app so once you add the EF nuget you could just copy/paste.
But the below code works with .net framework 4.6 and EF6.2 It creates the following tables
Car
Id (PK, int, not null)
Driver_Id (FK, int, null)
Driver
Id (PK, int, not null)
Under this schema a Car can have only one driver. A driver may still drive multiple cars though. I'm not sure if that's an issue for you or not.
using System.Data.Entity;
namespace EFTest
{
class Program
{
static void Main(string[] args)
{
var connectionString = "<your connection string>";
var context = new DatabaseContext(connectionString);
var car = new Car();
var driver = new Driver();
context.Cars.Add(car);
context.Drivers.Add(driver);
car.Driver = driver;
context.SaveChanges();
}
}
public class Car
{
public int Id { get; set; }
public virtual Driver Driver { get; set; }
}
public class Driver
{
public int Id { get; set; }
}
public class DatabaseContext : DbContext, IDatabaseContext
{
public DbSet<Car> Cars { get; set; }
public DbSet<Driver> Drivers { get; set; }
public DatabaseContext(string connectionString) : base(connectionString){ }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Car>()
.HasKey(n => n.Id)
.HasOptional(n => n.Driver);
modelBuilder.Entity<Driver>()
.HasKey(n => n.Id);
}
}
}
But if you REALLY wanted to enforce the constraint of only one mapping per car and driver, you could do it with the code below. Note that when you have the joining entity, you don't put it's Id anywhere on the joined entities.
using System.Data.Entity;
namespace EFTest
{
class Program
{
static void Main(string[] args)
{
var connectionString = "your connection string";
var context = new DatabaseContext(connectionString);
//Create a car, a driver, and assign them
var car = new Car();
var driver = new Driver();
context.Cars.Add(car);
context.Drivers.Add(driver);
context.SaveChanges();
var assignment = new DriverAssignment() { Car_id = car.Id, Driver_Id = driver.Id };
context.DriverAssignments.Add(assignment);
context.SaveChanges();
//Create a new car and a new assignment
var dupCar = new Car();
context.Cars.Add(dupCar);
context.SaveChanges();
var dupAssignment = new DriverAssignment() { Car_id = dupCar.Id, Driver_Id = driver.Id };
context.DriverAssignments.Add(dupAssignment);
//This will throw an exception because it will violate the unique index for driver. It would work the same for car.
context.SaveChanges();
}
}
public class Car
{
public int Id { get; set; }
}
public class Driver
{
public int Id { get; set; }
}
public class DriverAssignment
{
public int Car_id { get; set; }
public int Driver_Id { get; set; }
}
public class DatabaseContext : DbContext, IDatabaseContext
{
public DbSet<Car> Cars { get; set; }
public DbSet<Driver> Drivers { get; set; }
public DbSet<DriverAssignment> DriverAssignments { get; set; }
public DatabaseContext(string connectionString) : base(connectionString) { }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Car>().HasKey(n => n.Id);
modelBuilder.Entity<Driver>().HasKey(n => n.Id);
modelBuilder.Entity<DriverAssignment>().HasKey(n => new { n.Car_id, n.Driver_Id });
modelBuilder.Entity<DriverAssignment>().HasIndex(n => n.Car_id).IsUnique();
modelBuilder.Entity<DriverAssignment>().HasIndex(n => n.Driver_Id).IsUnique();
}
}
}
ASP.NET MVC and I got an issue that I don't know how to solve
I have a Vehicle class in model
VehicleId
Register
.... Etc
And a Driver class
DriverId
FirstName
LastName
I want to create a new table in order to control of what vehicle have a Driver where each Vehicle cannot have more than one Driver
I think of creating a table DriverVehicle with the key from Vehicle and Driver. Any help related with this would be a big help. I am working with ASP.NET MVC and EF.
What you're looking for is a mutually optional 1:1 association between independent entities (more exactly: 0..1-0..1). As I explained here, EF isn't really helpful here, but there are some options.
The first option is to settle with a 1-0..1 association, which is fully supported by EF. But either Car or Driver should have a primary key that's also a foreign key to the other entity. From what I read, this is not what you want. You want both entities to be able to exist independent of the other.
So the only option is, as you describe, introducing a third entity, aka a junction class. Now, Entity Framework only supports this for many-to-many associations, not for 1:1. So you'll have to map the association as many-to-many, but take some special measures to prevent duplicate associations on both ends.
To achieve that, this is what I came up with:
public class Car
{
public Car()
{
CarDrivers = new HashSet<CarDriver>();
}
public int CarId { get; set; }
public string Name { get; set; }
public virtual ICollection<CarDriver> CarDrivers { get; set; }
}
public class Driver
{
public Driver()
{
CarDrivers = new HashSet<CarDriver>();
}
public int DriverId { get; set; }
public string Name { get; set; }
public virtual ICollection<CarDriver> CarDrivers { get; set; }
}
public class CarDriver
{
[Key, Column(Order = 0), Index(IsUnique = true)]
public int CarId { get; set; }
[Key, Column(Order = 1), Index(IsUnique = true)]
public int DriverId { get; set; }
public Car Car { get; set; }
public Driver Driver { get; set; }
}
This is an explicit many-to-many association (i.e. having a visible junction class), but restricted to 1 at both ends by the unique indexes on both foreign keys in CarDriver.
So at least at the database side, the proper multiplicity of the association is enforced. However, in your code, nothing stops you from adding more than one CarDriver to the CarDrivers collections. This will throw an ugly database constraint error, so you'll have to guard this with custom validation.
Add DriverId as a foreign column in Vehicle table and add virtual list of Vehicles for a driver. So each vehicle will have single driver and each driver can drive many vehicles.
Vehicle Class
public virtual Driver Driver { get;set; }
public virtual int DriverId { get;set; } //make it null if you think some vechile dont have driver
Driver Class
public virtual IList<Vehicle> Vehicles { get;set; }
I'm trying to create what I think would be either called an optional 1:1 or possibly 0..1:0..1 relationship in Entity Framework. I want to be able to have navigation properties on both objects.
I am using Entity Framework's Fluent API over an existing database schema.
For simplicity, lets assume the following tables:
Car
Id int not null
Driver
Id int not null
CarId int null unique
Using the following classes:
public class Car
{
public int Id { get; set; }
public virtual Driver { get; set; }
}
public class Driver
{
public int Id { get; set; }
public virtual Car { get; set; }
}
The idea is a Car and a Driver can exist independent of one another, but when a Driver gets associated with a Car it is a mutually exclusive association: the Driver can only be associated with that Car and that Car can only be associated to that Driver.
I tried the following fluent configuration:
Inside Driver's Configuration:
HasOptional(d => d.Car)
.WithOptionalDependent()
.Map(d => d.MapKey("CarId"));
And inside the Car configuration
HasOptional(c => cDriver)
.WithOptionalPrincipal()
.Map(d => d.MapKey("CarId"));
When I try this I get the following:
Schema specified is not valid. Errors:
(203,6) : error 0019: Each property name in a type must be unique. Property name 'CarId' was already defined.
Is there a way to model this scenario with navigation properties on both objects in Entity Framework?
You don't need to set it up in both fluent classes. I'm surprised that is the error that you received, and not that the relationship is already set up.
Your Drive class will need the CarId as part of the class:
public class Driver
{
public int Id { get; set; }
// Make this int? if a Driver can exist without a Car
public int CarId { get; set; }
public virtual Car { get; set; }
}
Then you just need this in the Fluent Config file for Driver, and nothing in the one for Car.
HasOptional(d => d.Car)
.WithOptionalDependent()
.Map(d => d.MapKey("CarId"));
You can do this without Fluent API:
public class Car
{
public int Id { get; set; }
public string Name { get; set; }
public int? DriverId { get; set; }
[ForeignKey("DriverId")]
public virtual Driver Driver { get; set; }
}
public class Driver
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Car> Cars { get; set; }
}
Then you need to check if the Driver already has a car, to guarantee that he can have only one.
Hi I try use Many to Many relationship with EF Fluent API. I have 2 POCO classes.
public class Project
{
public int ProjectId { get; set; }
public virtual ICollection<Author> Authors { get; set; }
public Project()
{
Authors = new List<Author>();
}
}
public class Author
{
public int AuthorId { get; set; }
public virtual ICollection<Project> Projects { get; set; }
public Author()
{
Projects = new List<Project>();
}
}
And I map many to many relationship with this part of code:
////MANY TO MANY
modelBuilder.Entity<Project>()
.HasMany<Author>(a => a.Authors)
.WithMany(p => p.Projects)
.Map(m =>
{
m.ToTable("ProjectAuthors");
m.MapLeftKey("ProjectId");
m.MapRightKey("AuthorId");
});
This created table ProjectsAuthors in DB. It is my first attempt with this case of relationship mapping.
If I omitted this mapping it created table AuthorProject with similar schema. It is correct bevahior?
By trial and error I found the following. Given two classes...
public class AClass
{
public int Id { get; set; }
public ICollection<BClass> BClasses { get; set; }
}
public class BClass
{
public int Id { get; set; }
public ICollection<AClass> AClasses { get; set; }
}
...and no Fluent mapping and a DbContext like this...
public class MyContext : DbContext
{
public DbSet<AClass> AClasses { get; set; }
public DbSet<BClass> BClasses { get; set; }
}
...the name of the created join table is BClassAClasses. If I change the order of the sets...
public class MyContext : DbContext
{
public DbSet<BClass> BClasses { get; set; }
public DbSet<AClass> AClasses { get; set; }
}
...the name of the created join table changes to AClassBClasses and the order of the key columns in the table changes as well. So, the name of the join table and the order of the key columns seems to depend on the order in which the entity classes are "loaded" into the model - which can be the order of the DbSet declarations or another order if more relationship are involved - for example some other entity refering to AClass.
In the end, it doesn't matter at all, because such a many-to-many relationship is "symmetric". If you want to have your own name of the join table, you can specify it in Fluent API as you already did.
So, to your question: Yes, naming the join table AuthorProjects is correct behaviour. If the name had been ProjectAuthors it would be correct behaviour as well though.