如何建立松散的关系模型 [英] How to model loose relationships

查看:93
本文介绍了如何建立松散的关系模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设以下表格:

GL_Account(FacilityId,SubAccountId,CostCenterId,AccountTypeId),其中密钥为FacilityID,SubAccountId和CostCenterId。

GL_Account (FacilityId, SubAccountId, CostCenterId, AccountTypeId) where the key is FacilityID, SubAccountId, and CostCenterId.

Cost_Center(CostCenterId,Name),其中密钥为CostCenterId。

Cost_Center(CostCenterId, Name) where the key is CostCenterId.

Revenue_Center(RevenueCenterId,Name),其中密钥为RevenueCenterId。

Revenue_Center(RevenueCenterId, Name) where the key is RevenueCenterId.

 

GL_Account.CostCenterId字段有时是对Cost_Center表的引用,有时是基于AccountTypeId值(E,R)对Revenue_Center表的引用。显然数据库中没有外键,我们使用SQL正确连接。

The GL_Account.CostCenterId field is sometimes a reference to the Cost_Center table and sometimes a reference to the Revenue_Center table based on the AccountTypeId value (E, R). Obviously there is no foreign key in the database, we use SQL to join appropriately.

 

如果我的实体模型如下:

If my entity model is as follows:

公共类GLAccount

public class GLAccount

{

public int FacilityId {get;组; }

public int FacilityId {get; set; }

public int SubAccountId {get;组; }

public int SubAccountId {get; set; }

public int CostCenterId(get; set;}

public int CostCenterId (get; set; }

public int AccountTypeId {get; set;}

public int AccountTypeId {get; set;}

}

 

公共类CostCenter

public class CostCenter

{

public int CostCenterId {get; set;}

public int CostCenterId { get; set; }

public string Name {get; set;}

public string Name { get; set; }

}

 

公共类RevenueCenter

public class RevenueCenter

{

public int RevenueCenterId {get; set;}

public int RevenueCenterId { get; set; }

公共字符串名称{get; set;}

public string Name { get; set; }

}

 

有没有办法在GLAccount,CostCenter和RevenueCenter实体之间配置关系。 

Is there a way to configure a relationship between the GLAccount, CostCenter, and RevenueCenter entities. 

 

我尝试将CostCenter和RevenueCenter属性添加到GLAccount实体,并配置opt关系,但EF希望CostCenterId属性可以为空(int?),这使得EF不高兴,因为它是密钥的一部分。

I tried adding a CostCenter and RevenueCenter property to the GLAccount entity, and configuring an optional relationship, but EF wants the CostCenterId property to be nullable (int?) which makes EF unhappy because it's part of the key.

 

是否会添加一个抽象的ResponsibilityCenter类,CostCenter和RevenueCenter将从中派生帮助? 

Would adding an abstract ResponsibilityCenter class from which CostCenter and RevenueCenter derive help? 

 

我们将不胜感激任何想法或意见。  ;

Any ideas or comments would be appreciated. 

 

此外,这个数据模型是遗留的,只有在我牺牲了我的第一个出生时才能改变,我不愿意在这一点:)下周可能是一个不同的故事。

Also, this data model is legacy, and can only be changed if I sacrifice my first born, which I am unwilling to do at this point :) Next week may be a different story.

推荐答案

这个模型带回了我之前工作的回忆:)

This model brings back memories from my previous job :)

在Code First(或一般的EF)中,没有办法对本机进行建模。不幸的是,最好的选择可能是不建模关系并始终根据内存中的fk值进行连接。

There isn't a way to model this natively in Code First (or EF in general). Unfortunately the best option is probably to not model the relationship and always do joins based on fk values in memory.

您可以查看使用视图聚合来自成本和收入中心的数据告诉Code First它只是一张桌子。但是你必须开始思考视图如何处理插入,它可能会变得非常美元。

You could look at using a view to aggregate the data from Cost and Revenue Centers and telling Code First that it's just a table. But you would have to start mucking around with how inserts are processed by the view and it's probably going to get pretty messy.

~Rowan


这篇关于如何建立松散的关系模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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