I am trying to populate a List<Dock> when Profile.MatchId == Dock.MatchId from different classes.
This is a stripped down version of both classes:
public class Profile
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ProfileId { get; set; }
public int MatchId { get; set; }
public virtual List<Dock> Docks { get; set; } = new List<Dock>();
}
public class Dock
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int DockId { get; set; }
public int MatchId { get; set; }
}
How do I get the List<Dock> Docks navigation properties to correctly populate? I am believe I need to wire this up via modelBuilder - however have not done this before.
Good Time! There's a pretty simple example of model relationship configuration.
public class SomeContext : DbContext
{
public DbSet<Profile> Profiles { get; set; }
public DbSet<Dock> Docks { get; set; }
protected override Void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Dock>()
.HasOne(x => x.Profile)
.WithMany(x => x.Docks)
.HasForeignKey(x => x.ProfileId);
}
}
public class Profile
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ProfileId { get; set; }
public int MatchId { get; set; }
public virtual List<Dock> Docks { get; set; } = new List<Dock>();
}
public class Dock
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int DockId { get; set; }
public int MatchId { get; set; }
public int ProfileId { get; set; }
public Profile Profile { get; set; }
}
Actually you can't get values for List<> Docks in Profile, as you don't have direct connection between Profile and Docks.
You may try with this.
public class Profile
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ProfileId { get; set; }
public virtual List<Dock> Docks { get; set; } = new List<Dock>();
}
public class Dock
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int DockId { get; set; }
[ForeignKey("Profile")]
public int ProfileId { get; set; }
public Profile Profile {get; set; }
}
Related
I got Error on Entity framework insert. When I want to add records in sub table I got field not found error.
This is my main table class as below:
public class Rcp
{
public Rcp()
{
RcpFl = new HashSet<RcpFiles>();
}
[Column("Rcp_ID")]
public int RcpId { get; set; }
public String RcpTitle { get; set; }
public virtual ICollection<RcpFiles> RcpFl { get; set; }
}
And this is my sub table class:
public class RcpFiles
{
[Key]
public int RcpFile_Id { get; set; }
public byte[] Photo { get; set; }
public virtual Rcp rcp { get; set; }
}
On insert time I got error Field "RcpFile_Id" could not be found.
You are relying on convention regarding property names. This is how you can specify the relationships using annotations.
public class Rcp
{
public Rcp()
{
RcpFl = new HashSet<RcpFiles>();
}
[Key]
[Column("Rcp_ID")]
public int RcpId { get; set; }
public String RcpTitle { get; set; }
[InverseProperty(nameof(RcpFiles.rcp))]
public virtual ICollection<RcpFiles> RcpFl { get; set; }
}
public class RcpFiles
{
[Key]
public int RcpFile_Id { get; set; }
public byte[] Photo { get; set; }
[Column("Rcp_ID")]
public int RcpId { get; set; }
[ForeignKey(nameof(RcpId))]
public virtual Rcp rcp { get; set; }
}
It seems you have one-to-many relationship.
First you must add foreign key property to RcpFiles entity:
public class RcpFiles
{
[Key]
public int RcpFile_Id { get; set; }
public byte[] Photo { get; set; }
public virtual Rcp rcp { get; set; }
public int RcpId { get; set; }
}
Then you have to set configuration. You have two options to do that:
Fist is Fluent API:
public class SchoolContext : DbContext
{
public DbSet<RcpFiles> RcpFiles { get; set; }
public DbSet<Rcp> Rcpes{ get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// configures one-to-many relationship
modelBuilder.Entity<RcpFiles>()
.HasRequired<Rcp>(s => s.rcp)
.WithMany(g => g.RcpFl)
.HasForeignKey<int>(s => s.RcpId);
}
}
The second one is Data annotiation: this part explained by #brian-parker (https://stackoverflow.com/users/1492496/brian-parker)
For more information you can see: https://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx
I have created two tabled called Customer and destination . CustomerCode is a primary key in Customer and Foreign key is Destination .when i delete a customer the destination will be deleted.
public class tblCustomerDetails
{
[Key]
public string CustomerCode { get; set; }
public string CustomerName { get; set; }
}
public class tblDestinationDetails
{
[Key]
public string DestinationCode { get; set; }
[ForeignKey("tblCustomerDetails")]
public string CustomerCode { get; set; }
public tblCustomerDetails tblCustomerDetails { get; set; }
public string DestinationName { get; set; }
}
public class tblOrderDetails
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int SrNo { get; set; }
public int OrderNo { get; set; }
[ForeignKey("tblCustomerDetails")]
public string CustomerCode { get; set; }
public tblCustomerDetails tblCustomerDetails { get; set; }
[ForeignKey("tblDestinationDetails")]
public string DestinationCode { get; set; }
public tblDestinationDetails tblDestinationDetails { get; set; }
}
Your probable model will be
public class Customer
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CustomerCode { get; set; }
public virtual Destination destination { get; set; }//relationship with Destination
}
public class Destination
{
public virtual Customer customer { get; set; }//relationship with Customer
[Key, ForeignKey("User")]
public int CustomerCode { get; set; }
}
You need to use the fluent API and add following code in DBContext
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>()
.HasOptional(d => d.Destination)
.WithOptionalDependent()
.WillCascadeOnDelete(true);
}
I have some classes:
public class Values : Entity
{
[Key]
public int Values_ID { get; set; }
[Required]
public string Values_Name { get; set; }
[Required]
public int ValuesNumeric { get; set; }
public virtual ICollection<ValuesMetrics> ValuesMetrics { get; set; }
}
public class GQMetric : Entity
{
[Key]
public int GQMetric_ID { get; set; }
[Required]
public string GQMetricName { get; set; }
[Required]
public int Importance_ID { get; set; }
public virtual List<GQMetricsQuestions> GQMetricsQuestions { get; set; }
public virtual ICollection<ValuesMetrics> ValuesMetrics { get; set; }
public virtual ImportanceScale ImportanceScale { get; set; }
}
I need to create many-to-many relationship to my own created class ValuesMetrics, not to automatically generated table by entity framework. I have tried a lot of solutions here, here and here but none of it did not work. Eventually, I did this:
public class ValuesMetrics : Entity
{
public int GQMetric_ID { get; set; }
public int Value_ID { get; set; }
public virtual GQMetric GQMetric { get; set; }
public virtual Values Values { get; set; }
}
FluentAPI:
modelBuilder.Entity<ValuesMetrics>()
.HasKey(c => new { c.GQMetric_ID, c.Value_ID });
modelBuilder.Entity<GQMetricsQuestions>()
.HasKey(c => new { c.GQMetric_ID, c.Question_ID });
but created table (ValuesMetrics) have an excessive relationship (GQMetrics_GQMetric_ID). I need only two primary keys from Values and GQMetrics tables
Can you advice me how to solve this problem? Thanks for any help!
Applied #Esteban 's solution from the link already referenced by you: Create code first, many to many, with additional fields in association table
Basically I did the following three changes:
Used POCO entities instead of inheriting from Entity class
Removed EF attributes, since we'll be using fluent API anyway
Changed fluent API configuration
Resulting code:
public class MyContext : DbContext
{
public DbSet<Values> Values { get; set; }
public DbSet<GQMetric> GqMetric { get; set; }
public DbSet<ValuesMetrics> ValuesMetrics { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Values>().HasKey(values => values.Values_ID);
modelBuilder.Entity<GQMetric>().HasKey(metric => metric.GQMetric_ID);
modelBuilder
.Entity<ValuesMetrics>()
.HasKey(valuesMetrics => new
{
valuesMetrics.Value_ID,
valuesMetrics.GQMetric_ID
});
modelBuilder
.Entity<ValuesMetrics>()
.HasRequired(valuesMetrics => valuesMetrics.Values)
.WithMany(valueMetrics => valueMetrics.ValuesMetrics)
.HasForeignKey(valueMetrics => valueMetrics.Value_ID);
modelBuilder
.Entity<ValuesMetrics>()
.HasRequired(valuesMetrics => valuesMetrics.GQMetric)
.WithMany(valueMetrics => valueMetrics.ValuesMetrics)
.HasForeignKey(valueMetrics => valueMetrics.GQMetric_ID);
base.OnModelCreating(modelBuilder);
}
}
public class Values
{
public int Values_ID { get; set; }
public string Values_Name { get; set; }
public int ValuesNumeric { get; set; }
public virtual ICollection<ValuesMetrics> ValuesMetrics { get; set; }
}
public class GQMetric
{
public int GQMetric_ID { get; set; }
public string GQMetricName { get; set; }
public virtual ICollection<ValuesMetrics> ValuesMetrics { get; set; }
}
public class ValuesMetrics
{
public int GQMetric_ID { get; set; }
public int Value_ID { get; set; }
public virtual GQMetric GQMetric { get; set; }
public virtual Values Values { get; set; }
}
I have 3 classes in my model as you can see below.
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string UserName { get; set; }
public ICollection<MartialArtUserProfile> MartialArtUserProfiles { get; set; }
}
[Table("MartialArt")]
public class MartialArt
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string IconPath { get; set; }
public string ImagePath { get; set; }
public ICollection<MartialArtUserProfile> MartialArtUserProfiles { get; set; }
}
public class MartialArtUserProfile
{
public int UserProfileId { get; set; }
public UserProfile UserProfile { get; set; }
public int MartialArtId { get; set; }
public MartialArt MartialArt { get; set; }
}
And I have a configuration class for many to many relationship as below:
public class MartialArtUserProfileConfiguration : EntityTypeConfiguration<MartialArtUserProfile>
{
public MartialArtUserProfileConfiguration()
{
HasKey(a => new { a.MartialArtId, a.UserProfileId });
HasRequired(a => a.MartialArt)
.WithMany(s => s.MartialArtUserProfiles)
.HasForeignKey(a => a.MartialArtId)
.WillCascadeOnDelete(false);
HasRequired(a => a.UserProfile)
.WithMany(p => p.MartialArtUserProfiles)
.HasForeignKey(a => a.UserProfileId)
.WillCascadeOnDelete(false);
}
}
After defining my entities an relation when I try to run Update-Database in Package Manager Console, it says:
One or more validation errors were detected during model generation:
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'MartialArtUserProfile' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'MartialArtUserProfiles' is based on type 'MartialArtUserProfile' that has no keys defined.
What am I doing wrong?
Thanks in advance,
If I understand you are simply trying to create a many to many with a transitive table. If so this is another way to approach this. Use Fluent API to map as below. You can change the UserProfileToMartialArt to whatever you want the table name to be. Instead of creating the MartialArtUserProfile model let EF create the middle ground for you. This also specifies your keys which should get you around the error.
modelBuilder.Entity<UserProfile>()
.HasMany(b => b.MartialArts)
.WithMany(a => a.UserProfiles)
.Map(m => m.MapLeftKey("MartialArtId")
.MapRightKey("UserProfileId")
.ToTable("UserProfileToMartialArt"));
In MartialArts Model put
public IList<UserProfile> UserProfiles { get; set; }
In UserProfile Model put
public IList<MartialArt> MartialArts { get; set; }
Try doing it like this:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string UserName { get; set; }
[InverseProperty("UserProfiles")]
public IList<MartialArt> MartialArts { get; set; }
}
[Table("MartialArt")]
public class MartialArt
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string IconPath { get; set; }
public string ImagePath { get; set; }
[InverseProperty("MartialArts")]
public IList<UserProfile> UserProfiles { get; set; }
}
In EntityFramework 6.1, you don't need to do any of this - just add collections of the two types to each class and everything falls into place.
public class UserProfile {
public int Id { get; set; }
public string UserName { get; set; }
public virtual ICollection<MartialArt> MartialArts { get; set; }
public UserProfile() {
MartialArts = new List<MartialArt>();
}
}
public class MartialArt {
public int Id { get; set; }
public string Name { get; set; }
// *snip*
public virtual ICollection<UserProfile> UserProfiles { get; set; }
public MartialArt() {
UserProfiles = new List<UserProfile>();
}
}
I am using Entity framework 4.1 in MVC 3 application. I have an entity where I have primary key consists of two columns ( composite key). And this is being used in another entity as foreign key. How to create the relationship ? In normal scnerios we use :
public class Category
{
public string CategoryId { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public string CategoryId { get; set; }
public virtual Category Category { get; set; }
}
but what if category has two columns key ?
You can use either fluent API:
public class Category
{
public int CategoryId1 { get; set; }
public int CategoryId2 { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public int CategoryId1 { get; set; }
public int CategoryId2 { get; set; }
public virtual Category Category { get; set; }
}
public class Context : DbContext
{
public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Category>()
.HasKey(c => new {c.CategoryId1, c.CategoryId2});
modelBuilder.Entity<Product>()
.HasRequired(p => p.Category)
.WithMany(c => c.Products)
.HasForeignKey(p => new {p.CategoryId1, p.CategoryId2});
}
}
Or data annotations:
public class Category
{
[Key, Column(Order = 0)]
public int CategoryId2 { get; set; }
[Key, Column(Order = 1)]
public int CategoryId3 { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
public class Product
{
[Key]
public int ProductId { get; set; }
public string Name { get; set; }
[ForeignKey("Category"), Column(Order = 0)]
public int CategoryId2 { get; set; }
[ForeignKey("Category"), Column(Order = 1)]
public int CategoryId3 { get; set; }
public virtual Category Category { get; set; }
}
I believe the easiest way is to use Data Annotation on the Navigation property like this:
[ForeignKey("CategoryId1, CategoryId2")]
public class Category
{
[Key, Column(Order = 0)]
public int CategoryId1 { get; set; }
[Key, Column(Order = 1)]
public int CategoryId2 { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
public class Product
{
[Key]
public int ProductId { get; set; }
public string Name { get; set; }
public int CategoryId1 { get; set; }
public int CategoryId2 { get; set; }
[ForeignKey("CategoryId1, CategoryId2")]
public virtual Category Category { get; set; }
}
In .NET Core and .NET 5 < the documentation only shows Data annotations (simple key).
https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-composite-key%2Csimple-key#foreign-key
However using the example from #LadislavMrnka you will get a error message like this:
System.InvalidOperationException: There are multiple properties with
the [ForeignKey] attribute pointing to navigation ''. To define a
composite foreign key using data annotations, use the [ForeignKey]
attribute on the navigation.
Using that error message you can write the code like this:
public class Product
{
[Key]
public int ProductId { get; set; }
public string Name { get; set; }
public int CategoryId2 { get; set; }
public int CategoryId3 { get; set; }
[ForeignKey("CategoryId2,CategoryId3")]
public virtual Category Category { get; set; }
}
Fluent API (composite key) example from Microsoft:
internal class MyContext : DbContext
{
public DbSet<Car> Cars { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Car>()
.HasKey(c => new { c.State, c.LicensePlate });
modelBuilder.Entity<RecordOfSale>()
.HasOne(s => s.Car)
.WithMany(c => c.SaleHistory)
.HasForeignKey(s => new { s.CarState, s.CarLicensePlate });
}
}
public class Car
{
public string State { get; set; }
public string LicensePlate { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public List<RecordOfSale> SaleHistory { get; set; }
}
public class RecordOfSale
{
public int RecordOfSaleId { get; set; }
public DateTime DateSold { get; set; }
public decimal Price { get; set; }
public string CarState { get; set; }
public string CarLicensePlate { get; set; }
public Car Car { get; set; }
}