c# - Entity Framework 4.1 Code First 和一对多映射问题

标签 c# entity-framework entity-framework-4 ef-code-first code-first

我在映射现有数据库时遇到问题。

2 个表(简化)

"SomeEntity"
Id int
Name nvarchar

"EntityProperty"
EntityId int
Name nvarchar

并且具有从实体到实体属性的一对多关系。

我如何使用 EF 4.1 Code First 映射它?

提前致谢。

已编辑 1:

好的)这是我的代码

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

在查询获取属性时使用 Include 时出现的问题:

列名称“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;}
}

创建 ICollection(在关系的“1”侧)应该足以设置 1:N 关系。它将在 EntityProperty 表中创建 SomeEntity_Id(或 SomeEntityId)列。

编辑:顺便说一句:如果你想启用延迟加载,你可以将该集合设置为虚拟。

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

编辑:

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

首先尝试这个..然后你可以再次添加那个 ICollection,为了简单起见,这次我没有包括它(你仍然查询属性..但是使用:context.EntityProperties.Where(x =>x.EntityId == X);)

关于c# - Entity Framework 4.1 Code First 和一对多映射问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5753937/

相关文章:

entity-framework - Entity Framework 花费大量时间初始化模型

c# - Entity Framework 4 TPH 继承,如何将一种类型更改为另一种类型?

c# - 通过游戏开发提高编程技能

c# - 将 System.Data.Entity.DynamicProxies 转换为 C# 中的(非代理)类

c# - 通过 IoC 影响带有属性的 AOP;代码味还是优雅?

c# - 如何访问 ASP.NET Entity Framework 中的旧实体值

silverlight-4.0 - 绑定(bind)到 EntityCollection : Silverlight 4 中第一项的属性

c# - Linq 和 EntityFramework 4 具有多个内部连接和嵌套子查询

c# - 设置STS但在webapp中保留formauthentication

c# - 从复杂的集合中获取最新的值(value)