实体框架4,代码唯一,关系 [英] entity framework 4, code-only, relationships

查看:222
本文介绍了实体框架4,代码唯一,关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何解决以下问题。
我需要一个从一个基类到另一个基类的关系,所以每个派生类都与同一个表有关系,在我的例子中称为Item。



由于这只是一个例子,它不反映我的程序。在实际程序中,与类Item的关系在不同的命名空间中。因此它不能在派生类中。



错误:



派生类型WebApplication1.Client。必须为根类型WebApplication1.Base注册密钥。

 命名空间WebApplication1 
{
public class Item
{
public int ItemID {get;组; }
}

public class Base
{
public int ID {get;组; }
public int ItemID {get;组; }

public Item Item {get;组;

}

public class Client:Base
{
public string Name {get;组; }

私人列表<项目> _projects = null;

public List< Project>项目
{
get
{
if(_projects == null)
_projects = new List< Project>();

return _projects;
}
}
}

public class Project:Base
{
public string Name {get;组; }

public int ClientId {get;组; }

public Client Client {get;组; }

}

public class Main
{
public static void Test()
{
ContextBuilder< ObjectContext> ContextBuilder = new ContextBuilder< ObjectContext>();

var itemConfig = new EntityConfiguration< Item>();
itemConfig.HasKey(p => p.ItemID);
itemConfig.Property(p => p.ItemID).IsIdentity();
ContextBuilder.Configurations.Add(itemConfig);

var clientConfig = new EntityConfiguration< Client>();
clientConfig.HasKey(p => p.ID);
clientConfig.Property(p => p.ID).IsIdentity();
clientConfig.Property(p => p.Name);
clientConfig.Relationship(p => p.Item).HasConstraint((p,c)=> p.ItemID == c.ItemID);
ContextBuilder.Configurations.Add(clientConfig);

var projectConfig = new EntityConfiguration< Project>();
projectConfig.HasKey(p => p.ID);
projectConfig.Property(p => p.ID).IsIdentity();
projectConfig.Property(p => p.Name);

projectConfig.Relationship(p => p.Item).HasConstraint((p,c)=> p.ItemID == c.ItemID);

projectConfig.Relationship(p => p.Client).FromProperty(p => p.Projects).HasConstraint((p,c)=> p.ClientId == c.ID );

ObjectContext objCtx = ContextBuilder.Create(new SqlConnection(@Data Source =(local); Initial Catalog = testa; Integrated Security = SSPI;));

if(!objCtx.DatabaseExists())
objCtx.CreateDatabase();

}
}
}


解决方案

看看在这里如何处理继承映射: http://blogs.msdn.com/b/efdesign/archive/2009/10/12/code-only-further-enhancements.aspx



对于基本的非关系型产品以及如何重用它们: https://danielwertheim.wordpress.com/2009/11/29/entity-framework-4- how-to-reuse-mappings-and-add-a-concurrency-token /


I can't figure out how to solve the following problem. What i need it a relationship from one base class to another, so that every derived class has a relationship with the same table, called 'Item' in my example.

Since this is just an example it doesn't reflect my program. In the real program the relationship with class Item is in a different namespace. Therefore it can't be in the derived class.

The error:

A key is registered for the derived type 'WebApplication1.Client'. Keys must be registered for the root type 'WebApplication1.Base'.

namespace WebApplication1
{
    public class Item
    {
        public int ItemID { get; set; }
    }

    public class Base
    {
        public int ID { get; set; }
        public int ItemID { get; set; }

        public Item Item { get; set; }

    }

    public class Client : Base
    {
        public string Name { get; set; }

        private List<Project> _projects = null;

        public List<Project> Projects
        {
            get
            {
                if (_projects == null)
                    _projects = new List<Project>();

                return _projects;
            }
        }
    }

    public class Project : Base
    {
        public string Name { get; set; }

        public int ClientId { get; set; }

        public Client Client { get; set; }

    }

    public class Main
    {
        public static void Test()
        {
            ContextBuilder<ObjectContext> ContextBuilder = new ContextBuilder<ObjectContext>();

            var itemConfig = new EntityConfiguration<Item>();
            itemConfig.HasKey(p => p.ItemID);
            itemConfig.Property(p => p.ItemID).IsIdentity();
            ContextBuilder.Configurations.Add(itemConfig);

            var clientConfig = new EntityConfiguration<Client>();
            clientConfig.HasKey(p => p.ID);
            clientConfig.Property(p => p.ID).IsIdentity();
            clientConfig.Property(p => p.Name);
            clientConfig.Relationship(p => p.Item).HasConstraint((p, c) => p.ItemID == c.ItemID);           
            ContextBuilder.Configurations.Add(clientConfig);

            var projectConfig = new EntityConfiguration<Project>();
            projectConfig.HasKey(p => p.ID);
            projectConfig.Property(p => p.ID).IsIdentity();
            projectConfig.Property(p => p.Name);

            projectConfig.Relationship(p => p.Item).HasConstraint((p, c) => p.ItemID == c.ItemID);

            projectConfig.Relationship(p => p.Client).FromProperty(p => p.Projects).HasConstraint((p, c) => p.ClientId == c.ID);

            ObjectContext objCtx = ContextBuilder.Create(new SqlConnection(@"Data Source=(local);Initial Catalog=testa;Integrated Security=SSPI;"));

            if (!objCtx.DatabaseExists())
                objCtx.CreateDatabase();

        }    
    }       
}

解决方案

Look at how do deal with inheritance mapping here: http://blogs.msdn.com/b/efdesign/archive/2009/10/12/code-only-further-enhancements.aspx

For basic non-relational proberties and how-to reuse them: https://danielwertheim.wordpress.com/2009/11/29/entity-framework-4-how-to-reuse-mappings-and-add-a-concurrency-token/

这篇关于实体框架4,代码唯一,关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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