实体框架中的单例数据库模式 [英] Singleton Database Pattern in Entity Framework

查看:106
本文介绍了实体框架中的单例数据库模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我在数据库中实现单例模式,我遇到了巨大的问题。

I've got an application where I'm implementing a singleton pattern in the database, and I'm running into huge problems.

我有下表,SingletonTable

I've got the following Table, SingletonTable

+-------------+-------+------------+
| Column Name | Type  | Allow Null |
+-------------+-------+------------+
| Id          | Int   |  No        |
| VarType     | Int   |  No        |
| TypeARef    | Int   |  Yes       |
| TypeBRef    | Int   |  Yes       | 
+-------------+-------+------------+

在EF中,它分为两种具体类型,TypeA和TypeB。 TypeARef在TypeA上,TypeBRef在TypeB上,VarType是鉴别符。 (VariableType,TypeARef,TypeBRef)上有一个唯一的索引。单例模式的想法是,我们只有一个给定值元组的数据库中有一行,所以每当我们添加对SingletonTable的引用时,我们首先检查一下该行是否存在,如果有的话,我们返回DB中的行,如果没有,我们创建行并返回对新创建的行的引用。我已经将它抽象成一个方法GetSingleton。

In the EF, this is broken down into two concrete types, TypeA and TypeB. TypeARef is on TypeA, TypeBRef is on TypeB, and VarType is the discriminator. There is a unique index on (VariableType, TypeARef, TypeBRef). The idea of the singleton pattern is that we only ever have 1 row in the database for a given value tuple, so whenever we add a reference to to SingletonTable, we first check to see if that row exists, if it does, we return the row in the DB, if it doesn't, we create the row and return a reference to the newly created row. I've abstracted this out into a method, GetSingleton.

问题是每当我使用GetSingleton的方法,我会得到以下异常:

The problem is whenever I use the method GetSingleton, I get the follwoing Exception:

测试方法UnitTests.CreateBasicData.CreateFTIData抛出异常:System.InvalidOperationException:该对象无法添加到EntityCollection或EntityReference。附加到ObjectContext的对象无法添加到与源对象不相关联的EntityCollection或EntityReference

我正在使用它

var newRow = new SomeType
{
        singletonValue = GetSingleton(new TypeB{ TypeBRef = Foo })
}

这个问题似乎是我不能添加一个不是已经在数据上下文中的数据上下文中。这是正确的吗?如何解决这个限制?

The problem appears to be that I can't add a reference from something not already in the data context to something that is in the data context. Is this right? How do I work around this limitation?

谢谢,
Roy

Thanks, Roy

推荐答案

当在实体框架中的对象上下文中执行查询时,返回的对象将自动附加到对象上下文中。

When a query is executed inside an object context in the Entity Framework, the returned objects are automatically attached to the object context.

您还可以将对象附加到从查询以外的其他来源获取的对象上下文中。您可能会附加先前已分离的对象,NoTracking查询返回的对象或在对象上下文之外获取的对象。您还可以附加存储在ASP.NET应用程序的视图状态中的对象,或已从远程方法调用或Web服务返回的对象。

You can also attach objects to an object context that are obtained from a source other than a query. You might attach objects that have been previously detached, objects that have been returned by a NoTracking query, or objects that have been obtained outside the object context. You can also attach objects that have been stored in the view state of an ASP.NET application or have been returned from a remote method call or Web service.

使用其中一个将对象附加到对象上下文的以下方法:

Use one of the following methods to attach the object to an object context:


  • 在ObjectContext上调用AddObject将对象添加到对象
    上下文。当对象是数据源中尚未存在
    的新对象时,请执行此操作。

  • Call AddObject on ObjectContext to add the object to the object context. Do this when the object is a new object that does not yet exist in the data source.

在ObjectContext上调用Attach以将对象附加到对象
上下文。当数据源
中的对象已经存在但当前未附加到上下文时,请执行此操作。有关详细信息,请参阅如何:附加相关对象(实体框架)。

Call Attach on ObjectContext to attach the object to the object context. Do this when the object already exists in the data source but is currently not attached to the context. For more information, see How to: Attach Related Objects (Entity Framework).

在ObjectContext上调用AttachTo将对象附加到特定的
实体在对象上下文中设置。如果对象有
null(Visual Basic中没有)EntityKey值。

Call AttachTo on ObjectContext to attach the object to a specific entity set in the object context. Also do this if the object has a null (Nothing in Visual Basic) EntityKey value.

< a href =http://msdn.microsoft.com/en-us/library/bb896271.aspx =nofollow noreferrer> http://msdn.microsoft.com/en-us/library/bb896271.aspx

这篇关于实体框架中的单例数据库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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