ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-06-26 17:54:57  阅读:258  来源: 互联网

标签: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进行映射?

Thx提前.

编辑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,entity-framework-4,ef-code-first,code-first
来源: https://codeday.me/bug/20190626/1296345.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有