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

发布于 2024-11-03 06:40:38 字数 1804 浏览 2 评论 0原文

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

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

当我使用“包含在查询中获取属性”时出现问题:

列名“SomeEntity_EntityId”无效。 列名“SomeEntity_EntityId”无效。

I have problem with mapping existing database.

2 tables (simplified)

"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?

Thx in advance.

Edited 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:

Invalid column name 'SomeEntity_EntityId'.
Invalid column name 'SomeEntity_EntityId'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慕烟庭风 2024-11-10 06:40:38
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);)

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. It will create SomeEntity_Id (or SomeEntityId) column in EntityProperty table.

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

極樂鬼 2024-11-10 06:40:38

我解决了问题。
我无法将简单的 PK 添加到关系表中。我在所有独特的字段上添加了复杂的 PK 并映射一对多。就这样。

I solved problem.
I cannot add simple PK to relational table. I added complex PK on all unique fields and map one-to-many. That's all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文