una que primaria modelo llave incluir hacer foranea externa developer datos crear compuesta como columnas clave agregar c# entity-framework ef-code-first foreign-keys composite-key

c# - que - clave compuesta como clave externa



que es una llave foranea en base de datos (2)

Creo que la manera más fácil es usar la anotación de datos en la propiedad de navegación de esta manera: [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; } }

Estoy usando Entity framework 4.1 en la aplicación MVC 3. Tengo una entidad donde tengo clave principal consta de dos columnas (clave compuesta). Y esto se está utilizando en otra entidad como clave foránea. ¿Cómo crear la relación? En scnerios normales usamos:

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; } }

pero ¿y si la categoría tiene dos columnas clave?


Puedes usar cualquiera de las API con fluidez:

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}); } }

O anotaciones de datos:

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; } }