小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

c# – 實(shí)體框架4.1代碼優(yōu)先和一對(duì)多映射問(wèn)題

 印度阿三17 2019-06-26

我有映射現(xiàn)有數(shù)據(jù)庫(kù)的問(wèn)題.

2桌(簡(jiǎn)化)

"SomeEntity"
Id int
Name nvarchar

"EntityProperty"
EntityId int
Name nvarchar

并且從實(shí)體到實(shí)體屬性具有一對(duì)多的關(guān)系.

我如何使用EF 4.1 Code First進(jìn)行映射?

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

在查詢獲取屬性時(shí)使用Include時(shí)出現(xiàn)問(wèn)題:

列名稱“SomeEntity_EntityId”無(wú)效.
列名稱“SomeEntity_EntityId”無(wú)效.

解決方法:

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

創(chuàng)建ICollection(在關(guān)系的“1”側(cè))應(yīng)足以設(shè)置1:N關(guān)系.它將在EntityProperty表中創(chuàng)建SomeEntity_Id(或SomeEntityId)列.

編輯:順便說(shuō)一句:如果要啟用延遲加載,可以將該集合設(shè)置為虛擬.

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

首先嘗試這個(gè)…然后你可以再次添加ICollection,這次我沒(méi)有包含它以保持簡(jiǎn)單(你還是一個(gè)查詢屬性..但是:context.EntityProperties.Where(x => x.EntityId == X);)

來(lái)源:https://www./content-1-270551.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多