none
关于Entity Framework 多对多的映射问题 RRS feed

  • 问题

  • public class Meter
        {
            public int Id { get; set; }
    
            public string Name { get; set; }
        }
    
    
        public class FieldType
        {
            public int Id { get; set; }
    
            public string Name { get; set; }
        }
    
    
        public class MeterFieldType
        {
            public int MId { get; set; }
    
            public int FId { get; set; }
    
            public string Unit { get; set; }
        }

    有两张表Meter和FieldType  他们俩是多对多的关系,  就出现了第三张表MeterFieldType关系表,因为关系需要,这两个id会对应出一个字段,叫做Unit,现在在Entity Framework这么做呢
    • 已移动 ThankfulHeart 2013年7月2日 7:01 EntityFramework问题
    2013年7月2日 6:37

答案

全部回复

  • 您好,

    根据您的情况,如果EF要出现多对多,那么一个条件就是你关联表中的字段必须是其它表的外键,并且都是联合主键。也就是说:

    public string Unit { get; set; }    //不能需要

    但是如果你一定需要保留这个内容,那么恐怕你只能创建一对多,一对多的关系了。


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年7月2日 7:07
  • 比喻数据有这些

    Meter表

    Id       Name

    m1         仪表1

    m2         仪表2

    FieldType

    id            Name

    f1             参数1

    f2             参数2

    MeterFieldType 

    MId             FId                 Unit

    m1              f1                    kg

    m1              f2                    kw/h

    m2              f1                    g

    m2              f2                    w/h   

    思路就这样的,MeterFieldType  既是关系表,也是确定一个字段的表,或许我表达有问题

    2013年7月2日 7:21
  • 那你还只能创建两个一对多:

    Meter和MeterFieldInfo一对多,关联是Id和MID。

    FieldType和MeterFieldInfo一对多,关联是Id和FID


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年7月2日 7:32
  • public class Meter { public int Id { get; set; } public string Name { get; set; }

    public ICollection<MeterFieldType> MeterFieldTypes{get;set;}

    } public class FieldType { public int Id { get; set; } public string Name { get; set; } } 关系表 public class MeterFieldType {

    public int id{get;set;} public Meter Meter{ get; set; } public FieldType FieldType{ get; set; }

    public ICollection<Meter > Meters{get;set;}

    public ICollection<FieldType> FieldTypes{get;set;} }

    meter中mapping

    HasMany(h => h.MeterFieldTypes).WithMany(w => w.Meters);

    MeterFieldType中mapping

    HasRequired(a => a.Meter).WithOptional().Map(m => m.MapKey("MeterId"));


                HasRequired(a => a.FieldType).WithOptional().Map(m => m.MapKey("FieldTypeId"));

    2013年7月4日 4:39
  • 你完全可以这样尝试:

    public class Meter
        {
            public int Id { get; set; }
            public string Name { get; set; }
    	public ICollection<MeterFieldType> MeterFieldTypes{get;set;}
        }
    
    
        public class FieldType
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public ICollection<MeterFieldType> MeterFieldTypes{get;set;}
        }
    
        public class MeterFieldType
        {
    	public int id{get;set;}
            public Meter Meter{ get; set; }
            public FieldType FieldType{ get; set; }
        }

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年7月4日 5:40
  • public class meter
    {
       public int id;{get;set;}
       public string name;{get;set;}
    }
    
    public class FieldType
    {
       public int id;{get;set;}
       public string name{get;set;}
    }
    
    public class meterFieldType
    {
        public int id;{get;set;}
    
        public int mid;{get;set;}
    
        public int fid;{get;set;}
    }

    现在有两张表 一个meter和一个fieldType,多对多的关系,在数据库中的关系表是meterfieldType

    现在为了添加关联关系,我就手动改成了 上面的写法,写mapping。不知道这么写有没有问题

    2013年7月4日 6:10
  • 建议按照我的写法,可以不写Mapping了。

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年7月4日 6:12
  • 不用在meter中mapping

    HasMany(h => h.MeterFieldTypes).WithRequired(w => w.Meter);

    在fieldType中mapping

    HasMany(h => h.MeterFieldTypes).WithRequired(w => w.fieldType);

    在meterfieldType中mapping

    HasRequired(a => a.Meter).WithOptional().Map(m => m.MapKey("MeterId"));

    HasRequired(a => a.FieldType).WithOptional().Map(m => m.MapKey("FieldTypeId"));

    2013年7月4日 6:27
  • 你按照我给出的代码不行吗?还是?

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年7月4日 8:55