如何使用Entity Framework 5将PICO类与数据库中的特定表相关联 [英] How do i assosiate my PICO class with a specific table in my database with Entity Framework 5

查看:35
本文介绍了如何使用Entity Framework 5将PICO类与数据库中的特定表相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有一个表(多对多表),两个类A和B都是这样创建的.

I have a table in my db (a many to many table) that two classes A and B have created like this.

    this.HasMany(t => t.TrafficImageQuestions)
       .WithMany(t => t.TrafficImages)
        .Map(m =>
            {
                m.ToTable("TrafficImage_Answers");
                m.MapLeftKey("TrafficImagesGuid");
                m.MapRightKey("TrafficImageQuestionsId");


            });

现在,我想将我的自定义类与同一张表"TrafficImage_Answers"相关联,该类的原因是具有左右键,然后还有一个3. custom属性.

Now i would like to assosiate my custom class to this same table "TrafficImage_Answers", the class offcause have the left and right key and then also a 3. custom property.

(我确实将该列添加到了答案"数据库中)

(i did add the column to the database "Answer")

    public class TrafficImageAnswer
    {
        public System.Guid TrafficImageGuid { get; set; }
        public int TrafficImageQuestionId { get; set; }
        public byte Answer { get; set; }
    }

之所以这样做,是因为我希望实体模型能够跟踪A和B的多对多关系,并且仍然能够查找数据库中的3.属性Answer.

I am doing this as i want entity model to keep track of my many to many relationship of A and B and still be able to look up the 3. property Answer that is in the database.

我尝试过的事情

我尝试执行以下操作:

    this.Property(t => t.TrafficImageQuestionId)
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    this.Property(t => t.TrafficImageGuid)
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

    // Table & Column Mappings
    this.ToTable("TrafficImage_Answers");

但是我知道该表已经存在,逻辑上.我需要告诉它,它仅应使用该表,而不要尝试创建它.(即通过EF 5和程序包管理器中的数据库缓解措施来实现.)

But i get that the table already exists, logic. I need to tell it that it just should use that table and not try to create it. (im doing this with DB mitigrations in EF 5 and Package manager).

推荐答案

不支持.如果要在联结表中具有用于多对多关系的其他字段,则无法再将其映射为多对多关系.每个表只能映射一次,但是同时将表映射到实体和多对多关系会使它映射两次.

That is not supported. If you want to have additional field in the junction table for many-to-many relation you cannot map it as many-to-many any more. Each table can be mapped only once but mapping table to entity and to many-to-many relation in the same time makes it mapped twice.

您必须更改您的 TrafficImageQuestions TrafficImages 以使用与 TrafficImageAnswer 的一对多关系,而不是彼此之间的多对多关系

You must change your TrafficImageQuestions and TrafficImages to use one to many relations with TrafficImageAnswer instead of many-to-many relation with each other.

这篇关于如何使用Entity Framework 5将PICO类与数据库中的特定表相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆