EF 4.0仅代码从抽象到派生的关联 [英] EF 4.0 Code only assocation from abstract to derived

查看:85
本文介绍了EF 4.0仅代码从抽象到派生的关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅使用EF 4.0 Code我想在抽象类和普通类之间建立关联。

Using EF 4.0 Code only i want to make an assocation between an abstract and normal class.

我有类'Item','ContentBase'和'Test'。

I have class 'Item', 'ContentBase' and 'Test'.

'ContentBase'是抽象的,'Test'来自它。

'ContentBase' is abstract and 'Test' derives from it.

'ContentBase'有一个属性'Item',它链接到一个实例'item'。

'ContentBase' has a property 'Item' that links to an instance of 'Item'.

因此'Test.Item'或任何派生自'ContentBase'的类都有'Item'导航属性。

So that 'Test.Item' or any class that derives from 'ContentBase' has an 'Item' navigation property.

在我的数据库中,Test的每条记录都有一条匹配的记录。

In my DB every record for Test has a matching record for Item.

public class Item 
{ 
    public int Id { get; set;} 
} 
 
public abstract class ContentBase 
{ 
    public int ContentId { get; set;} 
    public int Id { get; set;} 
 
    public Item Item { get; set;} 
} 
 
public class Test : ContentBase 
{ 
    public string Name { get; set;} 
} 

现在有些初始代码

public void SomeInitFunction() 
{ 
 
    var itemConfig = new EntityConfiguration<Item>(); 
    itemConfig.HasKey(p => p.Id); 
    itemConfig.Property(p => p.Id).IsIdentity(); 
    this.ContextBuilder.Configurations.Add(itemConfig); 
 
    var testConfig = new EntityConfiguration<Test>(); 
    testConfig.HasKey(p => p.ContentId); 
    testConfig.Property(p => p.ContentId).IsIdentity(); 
 
    // the problem  
    testConfig.Relationship(p => p.Item).HasConstraint((p, q) => p.Id == q.Id); 
 
    this.ContextBuilder.Configurations.Add(testConfig);   
} 

这会产生错误:为派生类型"Test"注册了一个密钥。必须为根类型'ContentBase'注册密钥。

This gives an error: A key is registered for the derived type 'Test'. Keys must be registered for the root type 'ContentBase'.

无论如何,我尝试收到错误。我做错了什么?

anyway i try i get an error. What am i a doing wrong?

推荐答案



有人可以回答吗?我仍然
等待
答案。


这篇关于EF 4.0仅代码从抽象到派生的关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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