简体   繁体   English

实体框架4.1代码优先和一对多映射问题

[英]Entity Framework 4.1 Code First and One-to-Many mapping problem

I have problem with mapping existing database. 我有映射现有数据库的问题。

2 tables (simplified) 2桌(简化)

"SomeEntity"
Id int
Name nvarchar

and

"EntityProperty"
EntityId int
Name nvarchar

and have relation one-to-many from Entity to entity properties. 并且从实体到实体属性具有一对多的关系。

How I can map this using EF 4.1 Code First? 我如何使用EF 4.1 Code First进行映射?

Thx in advance. Thx提前。

Edited 1: 编辑1:

ok) this is my code 好的)这是我的代码

class Program
    {
        static void Main(string[] args)
        {
            var context = new DataContext();

            var result = context.SomeEntity.Include(p => p.EntityProperties);

            foreach (var entity in result)
            {
                Console.WriteLine(entity);
            }

        }
    }

    public class SomeEntity
    {
        public int EntityId { get; set; }
        public string Name { get; set; }
        public virtual ICollection<EntityProperty> EntityProperties { get; set; }

        public override string ToString()
        {
            return string.Format("Id: {0}, Name: {1}", EntityId, Name);
        }
    }

    public class EntityProperty
    {
        public int EntityId { get; set; }
        public string Name { get; set; }
    }

    public class DataContext : DbContext
    {
        public DbSet<SomeEntity> SomeEntity { get { return this.Set<SomeEntity>(); } }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<SomeEntity>().ToTable("SomeEntity");
            modelBuilder.Entity<SomeEntity>().HasKey(k => k.EntityId);

            modelBuilder.Entity<EntityProperty>().ToTable("EntityProperty");
            modelBuilder.Entity<EntityProperty>().HasKey(k => k.EntityId);
        }
    }

Problem when I use Include in query for get properties: 在查询获取属性时使用Include时出现问题:

Invalid column name 'SomeEntity_EntityId'. 列名称“SomeEntity_EntityId”无效。 Invalid column name 'SomeEntity_EntityId'. 列名称“SomeEntity_EntityId”无效。

public class SomeEntity
{
    public int SomeEntityId {get;set;}
    public string Name {get;set;}
    public ICollection<EntityProperty> EntityProperties {get;set;}
}

public class EntityProperty
{
    public int EntityPropertyId {get;set;}
    public string Name {get;set;}
}

Creating that ICollection (at '1' side of relation) should be enough to set 1:N relation. 创建ICollection(在关系的“1”侧)应足以设置1:N关系。 It will create SomeEntity_Id (or SomeEntityId) column in EntityProperty table. 它将在EntityProperty表中创建SomeEntity_Id(或SomeEntityId)列。

Edit: Btw: You can set that collection to virtual, if you want lazy loading enabled. 编辑:顺便说一句:如果要启用延迟加载,可以将该集合设置为虚拟。

public virtual ICollection<EntityProperty> EntityProperties {get;set}

Edit: 编辑:

public class SomeEntity
{
    [Key]
    public int Id {get;set;}
    public string Name {get;set;}
}

public class EntityProperty
{
    // What is PK here? Something like:
    [Key]
    public int Id {get;set;}

    // EntityId is FK
    public int EntityId {get;set;}

    // Navigation property
    [ForeignKey("EntityId")]
    public SomeEntity LinkedEntity {get;set;}

    public string Name {get;set;}
}

First try this.. then you can add that ICollection again, I didn't include it this time to keep it simple (and you an still query properties.. but with: context.EntityProperties.Where(x=>x.EntityId == X); ) 首先尝试这个..然后你可以再次添加ICollection,这次我没有包含它以保持简单(你还是一个查询属性..但是: context.EntityProperties.Where(x=>x.EntityId == X);

I solved problem. 我解决了问题。 I cannot add simple PK to relational table. 我无法向关系表添加简单的PK。 I added complex PK on all unique fields and map one-to-many. 我在所有唯一字段上添加了复杂的PK并映射了一对多。 That's all. 就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架代码优先 - 具有连接/链接表的一对多 - Entity Framework Code First - One-to-Many with a join/link table 首先与实体框架代码建立多个一对多关系 - Multiple one-to-many relationships with entity framework code first 实体框架代码优先:一对多循环引用 - Entity Framework Code First: One-to-many cyclic reference 一对多实体框架5首先更新代码 - One-To-Many Entity Framework 5 Update with code first 与代码优先实体框架的一对多关系 - One-to-many relationships with code-first Entity Framework 实体框架代码首先审核多对多和一对多的问题 - Entity Framework Code First Auditing many-to-many and one-to-many issues EF 4.1编码第一个多个一对多关联 - EF 4.1 Code first multiple one-to-many associations 实体框架6流利的映射-一对多代码优先 - Entity Framework 6 Fluent Mapping - One to Many Code First 实体框架5代码优先中的一对一和一对多关系 - Both One-To-One and One-To-Many relationships in Entity Framework 5 Code First 如何使用Entity Framework 4.1 Code First强制数据库中一对多关系的一对一关系 - How to use Entity Framework 4.1 Code First to force a one to one relationship for a one to many relationship in the database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM