简体   繁体   English

实体框架6关系

[英]Entity Framework 6 Relationship

Tools Used: 使用的工具:

WPF .Net 4.5
MySQL - Lastest version
Visual Studio 2013 Community Edition

Problem 问题

Hi, 你好

I'm having trouble mapping the POCO objects to the existing database where I have a base class that contains an ID property that is inherited by all model classes. 我在将POCO对象映射到现有数据库时遇到麻烦,在该数据库中我有一个基类,该基类包含由所有模型类继承的ID属性。

The base class itself is ignored in the context's OnModelCreating method and the problem starts when I try to specify the relationship between User and Session. 基类本身在上下文的OnModelCreating方法中被忽略,当我尝试指定User和Session之间的关系时,问题就开始了。

The tables are as follows: 下表如下:

====================
User
====================
ID | Name | Password
====================


====================
Session
====================
ID | Password | User
====================

The third column "User" in the session table, contains the user ID, so normally I could join the user table to the session table with plain old SQL. 会话表中的第三列“用户”包含用户ID,因此通常我可以使用普通的旧SQL将用户表连接到会话表。

All the examples I saw on the web involves creating a navigational property, first problem with that, it creates circular references which creates headaches when you try to serialize the object, and serialization is used to communicate between the client and the server. 我在Web上看到的所有示例都涉及创建导航属性,第一个问题是,它创建了循环引用,当您尝试序列化对象时会产生麻烦,并且序列化用于在客户端和服务器之间进行通信。

The second problem is that this is a toy example of a much bigger system, the objects would get polluted with lots of properties. 第二个问题是,这是一个更大系统的玩具示例,对象将被许多属性污染。

Is this example, the session object knows about the user but user does not know about his session. 在此示例中,会话对象知道用户,但用户不知道他的会话。 So far i tried to map the session with the following: 到目前为止,我尝试将会话映射为以下内容:

public
class SessionMap : EntityTypeConfiguration <Session>
{
    public
    SessãoMap ()
    {
        /**
         * Table.
         */
        this.ToTable ("Session");

        /**
         * Primary key.
         */
        this.HasKey (a => a.ID);

        /**
         * Columns.
         */
        this.Property (a => a.ID      );
        this.Property (a => a.Password);

        this.HasRequired (a => a.User)
            .WithMany    ();
    }
}

But as you can see, I'm not specifying the foreign key and it gives the following error when I try to save a session object: 但是正如您所看到的,我没有指定外键,并且在尝试保存会话对象时出现以下错误:

Unknown column 'User_ID' in 'field list'

Which makes sense but how do I solve this? 哪一个有意义,但是我该如何解决呢? :s :s

Reference Code 参考代码

public
abstract
class Base
{
    public Int64 ID {get;set;}
}

public
class User : Base
{
    public String Name     {get;set;}
    public String Password {get;set;}
}

public
class Session : Base
{
    public String Password {get;set;}
    public User   User     {get;set;}
}

public
class Context : DbContext
{
    public DbSet <Session> Sessions {get;set;}
    public DbSet <User>    Users    {get;set;}

    public
    Context () : base ("name=MySQLConnectionString")
    {
    }

    protected
    override
    void OnModelCreating (DbModelBuilder Builder)
    {
        base.OnModelCreating (Builder);

        Builder.Ignore <Base> ();

        Builder.Configurations.Add (new Mapping.SessionMap ());
        Builder.Configurations.Add (new Mapping.UserMap    ());
    }
}

Found the answer. 找到了答案。

In the session table, I changed the "User" column to "UserID" and then in the SessionMap class I did the following: 在会话表中,我将“用户”列更改为“ UserID”,然后在SessionMap类中执行以下操作:

this.HasRequired (a => a.User)
    .WithMany    ()
    .Map         (m => m.MapKey ("UserID"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM