多个“ObjectChangeTracker”被创建了,可以避免吗?

发布于 2024-10-09 07:00:50 字数 828 浏览 1 评论 0原文

我们正在开发一个 POC,其中我们具有以下架构(MVVM),

WPF(客户端)+ WCF + 模型(数据访问)+ ADO.Net Entity Framework 4.0(使用 SQL Server 2008 R2 作为数据库)

所有都是不同的项目。

在数据访问层中,我们根据功能创建了不同的实体模型(edmx)。特定流下的表被分组并创建不同的实体模型。我们使用自跟踪实体通过 wcf 服务来回与 WPF 客户端进行通信。对于单一模型,一切正常。但是当我们创建多个模型时,就很少出现问题了。多个模型几乎没有重复的表/实体。两个探针是,

1)当我们尝试访问来自不同模型的实体时,会创建多个对象“ObjectChangeTracker”。 例如 CompanyModel(edmx) - 公司(实体) - ObjectChangeTracker、ObjectState ProductModel(edmx) - 客户(实体) - ObjectChangeTracker1、ObjectState1 OrderModel(edmx) - Oder(Entity) - ObjectChangeTracker2、ObjectState2

有什么方法可以避免这种情况吗?

2)模型之间共享的表很少,例如公司(实体)用于以上所有 mdoels 中。在编译期间,它不会出现任何错误。但运行时它给出错误,指出“指定的架构无效。错误:CLR 类型到 EDM 类型的映射不明确,因为多个 CLR 类型与 EDM 类型“公司”匹配”。为了解决此问题,我们用一些名称重命名了实体前缀使它们独一无二。有没有其他方法可以解决这个问题,而无需更改同一程序集中实体的名称?

预先感谢并感谢任何人能够解决这些问题。

谢谢, 基兰

We are working on a POC where we have following architecture (MVVM),

WPF(Client) + WCF + Model(DataAccess)+ ADO.Net Entity Framework 4.0 (with SQL Server 2008 R2 as DB)

All are different projects.

In the DataAccess layer we have created different Entity Models(edmx) based on the functionality. The tables under perticular flow are grouped and created different entity models. We are using self tracking entities to and fro to communicate with the WPF client through wcf service. For Single model everything works fine. But when we created a Multiple models then few issues started coming. Mutliple models have few duplicate tables/entities. Two probels are,

1) When we try to access entities from different models mutiple objects "ObjectChangeTracker" are getting created.
E.g.
CompanyModel(edmx) - Company(Entity) - ObjectChangeTracker, ObjectState
ProductModel(edmx) - Customer(Entity) - ObjectChangeTracker1, ObjectState1
OrderModel(edmx) - Oder(Entity) - ObjectChangeTracker2, ObjectState2

Is there any way to avoid this?

2) There are few tables which shared across the Models, E.g. Company(Entity) is used in All above mdoels. During compile time it does not thow any error. But run time It gives error saying "Schema specified is not valid. Errors: The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type "Company"".. To resolve this, we renamed the entities with some prefix to make them Unique. Is there any other way we can resolve this without changing the name of the entity in the same assembly?

Thanks in advance and appreciate if anyone has approach for these issues.

Thanks,
Kiran

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单挑你×的.吻 2024-10-16 07:00:50

1) 当您从数据访问中获取实体时,您是否始终启用 ChangeTracker?
我猜你无法避免创建的内容,除非你使用 POCO 模板。在某些事情上它还有更多的工作要做,但你会有更轻的物体。然后您必须自己管理实体状态。我认为保持自我跟踪很好,但是当您使用 WCF 时,您应该将集合类型更改为 FixUpCollection,因为我记得更好地使用您的 WCF 服务。提示:不要忘记禁用延迟加载,否则当实体被序列化时,您最终将拥有您不需要的所有子记录。

2)尝试将模型分开在不同的组件中,是更好的做法,我认为你会克服这些问题。我就是这样工作的,没问题。

希望我有帮助....

1) Are you enabling the ChangeTracker always when you grab entities from your data access?
I guess you can't avoid what is created unless you use the POCO Template. It has some more job to be done in some matters but you will have lighter objects. You must then manage the entity state by yourself. I think its good to stay on selftracking but while you use WCF you should change the collection type to FixUpCollection as i can remember to work better with your WCF Service. HINT: Don't forget to disable lazy loading or else you will end up having all the child records you night not want when the entities are serialized.

2) Try and seperate the models in different assemblies, is a better practice and i think you will overcome these problems. I worked this way and its ok.

Hope i helped....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文