I have an entity model that contains multiple definitions to another entity. I can get one definition to work, but not both.
public class Inspection : Entity<int>
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int InspectionId { get; set; }
[ForeignKey("Report")]
public int ReportId { get; set; }
public virtual Report Report { get; set; }
....
public virtual ICollection<ResidentialDescriptionItem> ResidentialDescriptionItems { get; set; }
public virtual ICollection<ResidentialDescriptionItem> ResidentialOtherDescriptionItems { get; set; }
}
public class ResidentialDescriptionItem : Entity<int>
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ResidentialDescriptionItemId { get; set; }
public int InspectionId { get; set; }
[ForeignKey("InspectionId")]
public virtual Inspection Inspection { get; set; }
//public int Inspection1Id { get; set; }
//[ForeignKey("Inspection1Id")]
//public virtual Inspection Inspection1 { get; set; }
}
I've made numerous attempts with that second index and received just as many different errors. The above configuration results in
Unable to determine the principal end of the
'MySolution.EntityFramework.ResidentialDescriptionItem_Inspection'
relationship. Multiple added entities may have the same primary key.
I would like to maintain a full configuration with navigation on both sides. How do I do this using Code First and Annotations?
I don't think it is possible to implement such complex relationship with annotations, but here is a demo, how you would need to override your DbContext.OnModelCreating
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
namespace ConsoleApp3
{
public class Parent
{
[Key]
public int Id { get; set; }
public virtual ICollection<Child> Children { get; set; }
public virtual ICollection<Child> OtherChildren { get; set; }
}
public class Child
{
[Key]
public int Id { get; set; }
[ForeignKey("Parent")]
public int? ParentId { get; set; }
[ForeignKey("ParentId")]
public virtual Parent Parent { get; set; }
[ForeignKey("OtherParent")]
public int? OtherParentId { get; set; }
[ForeignKey("OtherParentId")]
public virtual Parent OtherParent { get; set; }
}
public class MyDbContext : DbContext
{
public MyDbContext(string nameOrConnectionString) : base(nameOrConnectionString)
{
}
public DbSet<Parent> Parents { get; set; }
public DbSet<Child> Children { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Parent>()
.HasMany(x => x.Children)
.WithOptional(x => x.Parent);
modelBuilder.Entity<Parent>()
.HasMany(x => x.OtherChildren)
.WithOptional(x => x.OtherParent);
}
}
class Program
{
static void Main(string[] args)
{
Database.SetInitializer(new DropCreateDatabaseAlways<MyDbContext>());
var ctx = new MyDbContext("Data Source=DESKTOP-5PVJ0I5;Database=test1;Integrated Security=true");
var parent = ctx.Parents.Add(new Parent());
ctx.Children.Add(new Child()
{
OtherParent = parent
});
ctx.Children.Add(new Child()
{
Parent = parent
});
ctx.SaveChanges();
parent = ctx.Parents
.Include(x => x.Children)
.Include(x => x.OtherChildren)
.FirstOrDefault();
}
}
}
Related
I am trying to create a Product table that will have a list of SubstitutionProducts that should reference other Product from the same table.
Model example :
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsMissing { get; set; }
public ICollection<Product> SubstitutionProducts { get; set; }
}
It is better to include the parent ID in your model:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsMissing { get; set; }
public int? ParentId { get; set; }
public Product Parent { get; set; }
public ICollection<Product> SubStitutionProducts { get; set; }
}
This is how to configure DB schema via overriding OnModelCreatingmethod of your DbContext:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().HasOne(x => x.Parent)
.WithMany(x => x.SubStitutionProducts)
.HasForeignKey(x => x.ParentId).IsRequired(false);
}
I'm attempting to set this up using code first in entity framework and am running into difficulty. To describe what i'm trying to accomplish:
Have an entity of Product. This product optionally may have one or more related "child" products. A product can be the child to one or more parent products.
when I go to generate a controller tied to the model class "Product", i'm getting an error: (updated, more specific, matches code below)
There was an error running the selected code generator:
'Unable to retrieve metadata for 'ProductCatalog.Models.Product'.
Multiple object sets per type are not supported. The object sets
'Product' and 'Products' can both contain instances of type
'ProductCatalog.Models.Product'.
here's the not working model class:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace ProductCatalog.Models
{
// Product
public class Product
{
[Key]
public int ProductId { get; set; } // ProductID (Primary key)
public string ProductName { get; set; } // ProductName
public string ProductSku { get; set; } // ProductSKU
public int BaseQuantity { get; set; } // BaseQuantity
public decimal BaseCost { get; set; } // BaseCost
// Reverse navigation
public virtual ICollection<RelatedProduct> ParentProducts { get; set; } // RelatedProduct.FK_RelatedProductChildID
public virtual ICollection<RelatedProduct> ChildProducts { get; set; } // RelatedProduct.FK_RelatedProductParentID
public virtual ICollection<RelatedProduct> RelatedProducts { get; set; }
}
// RelatedProduct
public class RelatedProduct
{
[Key, Column(Order = 0)]
public int ParentId { get; set; } // ParentID
[Key, Column(Order = 1)]
public int ChildId { get; set; } // ChildID
public int Quantity { get; set; } // Quantity
public bool Required { get; set; } // Required
public bool Locked { get; set; } // Locked
// Foreign keys
public virtual Product ParentProduct { get; set; } // FK_RelatedProductParentID
public virtual Product ChildProduct { get; set; } // FK_RelatedProductChildID
}
public class ProductDBContext : DbContext
{
public IDbSet<Product> Product { get; set; } // Product
public IDbSet<RelatedProduct> RelatedProduct { get; set; } // RelatedProduct
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<RelatedProduct>()
.HasRequired(a => a.ParentProduct)
.WithMany(b => b.ChildProducts)
.HasForeignKey(c => c.ParentId) // FK_RelatedProductParentID
.WillCascadeOnDelete(false);
modelBuilder.Entity<RelatedProduct>()
.HasRequired(a => a.ChildProduct)
.WithMany(b => b.ParentProducts)
.HasForeignKey(c => c.ChildId); // FK_RelatedProductChildID
}
}
}
fixed by pluralizing the DbSets
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace ProductCatalog.Models
{
// Product
public class Product
{
[Key]
public int ProductId { get; set; } // ProductID (Primary key)
public string ProductName { get; set; } // ProductName
public string ProductSku { get; set; } // ProductSKU
public int BaseQuantity { get; set; } // BaseQuantity
public decimal BaseCost { get; set; } // BaseCost
// Reverse navigation
public virtual ICollection<RelatedProduct> ParentProducts { get; set; } // RelatedProduct.FK_RelatedProductChildID
public virtual ICollection<RelatedProduct> ChildProducts { get; set; } // RelatedProduct.FK_RelatedProductParentID
public virtual ICollection<RelatedProduct> RelatedProducts { get; set; }
}
// RelatedProduct
public class RelatedProduct
{
[Key, Column(Order = 0)]
public int ParentId { get; set; } // ParentID
[Key, Column(Order = 1)]
public int ChildId { get; set; } // ChildID
public int Quantity { get; set; } // Quantity
public bool Required { get; set; } // Required
public bool Locked { get; set; } // Locked
// Foreign keys
public virtual Product ParentProduct { get; set; } // FK_RelatedProductParentID
public virtual Product ChildProduct { get; set; } // FK_RelatedProductChildID
}
public class ProductDBContext : DbContext
{
public IDbSet<Product> Products { get; set; } // Product
public IDbSet<RelatedProduct> RelatedProducts { get; set; } // RelatedProduct
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<RelatedProduct>()
.HasRequired(a => a.ParentProduct)
.WithMany(b => b.ChildProducts)
.HasForeignKey(c => c.ParentId) // FK_RelatedProductParentID
.WillCascadeOnDelete(false);
modelBuilder.Entity<RelatedProduct>()
.HasRequired(a => a.ChildProduct)
.WithMany(b => b.ParentProducts)
.HasForeignKey(c => c.ChildId); // FK_RelatedProductChildID
}
}
}
I am having difficulty maintaining multiple relationships between a parent class and it's children. Can anyone tell me why I can create two child references in the parent but not a third? The code below only works when the third reference is commented out.
public class Parent
{
public int Id { get; set; }
public string Name { get; set; }
public int Child1Id { get; set; }
public Child Child1 { get; set; }
public int Child2Id { get; set; }
public Child Child2 { get; set; }
//public int Child3Id { get; set; }
public Child Child3 { get; set; }
public ICollection<Child> Children { get; set; }
}
public class Child
{
public int Id { get; set; }
public string Name { get; set; }
public int ParentId { get; set; }
public Parent Parent { get; set; }
}
public class CFContext : DbContext
{
public DbSet<Parent> Parents { get; set; }
public DbSet<Child> Children { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Child>()
.HasRequired(c => c.Parent)
.WithRequiredPrincipal(p => p.Child1)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Child>()
.HasRequired(c => c.Parent)
.WithRequiredPrincipal(p => p.Child2)
.WillCascadeOnDelete(false);
//modelBuilder.Entity<Child>()
// .HasRequired(c => c.Parent)
// .WithRequiredPrincipal(p => p.Child3)
// .WillCascadeOnDelete(false);
}
}
It looks like you are trying to make a one-to-many relation from Parent to Child entity. In that case the code should look like this:
public class Parent
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Child> Children { get; set; }
}
public class Child
{
public int Id { get; set; }
public string Name { get; set; }
public int ParentId { get; set; }
public Parent Parent { get; set; }
}
You don't have to specify the relation in Fluent API as long as you are following the default conventions regarding naming of the navigation properties and foreign key. You will have to use Fluent API and/or attributes to configure relations of you use non-convention names, eg renaming ParentId some something else requires you to mark it with at [ForeignKey("Parent")] attribute.
The most common use case for using Fluent API is for disabling cascade delete (there is no way to do this with attributes).
The following code creates foreign key errors when all code is not commented.
public class Parent
{
public int Id { get; set; }
public string Name { get; set; }
public int FavoriteChildId { get; set; }
public Child FavoriteChild { get; set; }
//public int WorstChildId { get; set; }
public Child WorstChild { get; set; }
public ICollection<Child> Children { get; set; }
}
public class Child
{
public int Id { get; set; }
public string Name { get; set; }
//public int ParentId { get; set; }
public Parent Parent { get; set; }
}
public class CFContext : DbContext
{
public DbSet<Parent> Parents { get; set; }
public DbSet<Child> Children { get; set; }
}
It works if the foreign key names aren't specified but then there's no way to edit the models. Does anyone know how to specify the foreign key names?
Following the naming convention will create the proper FK's above - your code:
public int WorstChildId { get; set; }
public Child WorstChild { get; set; }
Does create the FK of WorstChildId for WorstChild. However, when I tried the code you had above, I got a multiple delete path error (Parent -> WorstChild -> ChildTable, Parent -> FavoriteChild -> ChildTable)
You can set either one, or both of your mappings to not cascade on delete, and that will fix your problem:
public class CFContext : DbContext
{
public DbSet<Parent> Parents { get; set; }
public DbSet<Child> Children { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Child>()
.HasRequired(c => c.Parent)
.WithRequiredPrincipal(p => p.WorstChild)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Child>()
.HasRequired(c => c.Parent)
.WithRequiredPrincipal(p => p.FavoriteChild)
.WillCascadeOnDelete(false);
}
}
This code doesn't work. What is wrong? OnModelCreating doesnt't effect any result? Because I can not see "ProductCategories" table in my Database.
public class GoldContext : DbContext
{
public virtual DbSet<Prouct> Products { get; set; }
public virtual DbSet<Category> Categories { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//HACK:4.1 modelBuilder.Conventions.Add(new DecimalPrecisionAttributeConvention());
modelBuilder.Entity<Product>()
.HasMany<Category>(m => m.Categories)
.WithMany().Map(m =>
m.MapLeftKey("ProductId")
.MapRightKey("CategoryId")
.ToTable("ProductCategories"));
base.OnModelCreating(modelBuilder);
}
}
//product and category classes look like this.
public class Product
{
[Key]
public int Id { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<Category> Categories { get; set; }
}
public class Category
{
[Key]
public int Id { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
Thanks in advance.
This is what I've tried in a console application and works as expected :
namespace Q7122388
{
#region Imports
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
#endregion
public class Product
{
[Key]
public int Id { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<Category> Categories { get; set; }
}
public class Category
{
[Key]
public int Id { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
public class DatabaseContext : DbContext
{
public virtual DbSet<Product> Products { get; set; }
public virtual DbSet<Category> Categories { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>()
.HasMany<Category>(m => m.Categories)
.WithMany().Map(m =>
m.MapLeftKey("ProductId")
.MapRightKey("CategoryId")
.ToTable("ProductCategories"));
base.OnModelCreating(modelBuilder);
}
}
class Program
{
static void Main(string[] args)
{
Database.SetInitializer(new DropCreateDatabaseAlways<DatabaseContext>());
using (var context = new DatabaseContext())
context.Database.Initialize(true);
}
}
}