简体   繁体   English

实体框架v4 - >需要POCO和实体的一些帮助

[英]Entity Framework v4 -> need some help with POCO's and Entities

I'm using EF4 and I've got two entities that I wish to map to the same POCO . 我正在使用EF4,我有两个实体,我希望映射到同一个 POCO I'm not sure how I can do this. 我不知道怎么能这样做。

Entity 1 → Foo (this represents a table FOO in the db) 实体1→Foo(这表示数据库中的表FOO)
POCO → Foo POCO→Foo

Entity 2 → FooView (this represents a view FooView in the db) 实体2→FooView(这表示数据库中的FooView视图)
POCO → Foo POCO→Foo

I understand that I need to do something like 我明白我需要做点什么

IObjectSet<Foo> _foos = CreateObjectSet<Foo>();

// Note spelling of the Entity.
IObjectSet<Foo> _foosView = CreateObjectSet<Foo>("FooViews"); 

But when i try this, it does compile, but it fails with the following exception: 但是当我尝试这个时,它确实编译,但它失败并出现以下异常:

System.ArgumentException: System.ArgumentException: The specified entity type, 'MyProject.Core.Foo', does not match the type 'EntityFramework.SqlServerModel.FoosView' from the EntitySet 'FoosViews'. System.ArgumentException:System.ArgumentException:指定的实体类型“MyProject.Core.Foo”与EntitySet“FoosViews”中的“EntityFramework.SqlServerModel.FoosView”类型不匹配。

Any suggestions? 有什么建议?

Here is a checklist of things to look for: 以下是要查找的事项清单:

  1. Your Storage Model should have: 您的存储模型应具有:
    1. Two EntitySets : Foo and FooView 两个EntitySetsFooFooView
    2. Two EntityTypes : Foo and FooView 两个EntityTypesFooFooView
  2. Your Conceptual Model should have: 您的概念模型应该具有:
    1. Two EntitySets : Foo and FooView - both with an EntityType set to ModelName.Foo 两个EntitySetsFooFooView - 两者EntityType设置为ModelName.Foo
    2. One EntityType : Foo 一个EntityTypeFoo
  3. Your Mapping should have two EntitySetMappings : 您的映射应该有两个EntitySetMappings
    1. Foo with one EntityTypeMapping ("ModelName.Foo") with one MappingFragment ("Foo") Foo与一个EntityTypeMapping (“ModelName.Foo”)与一个MappingFragment (“Foo”)
    2. FooView with one EntityTypeMapping ("ModelName. Foo ") with one MappingFragment ("FooView") FooView与一个EntityTypeMapping ( “MODELNAME。 ”)与一种MappingFragment (“ FooView”)

You should new be able to execute the following: 你应该新的能够执行以下内容:

Foo foo = new ModelEntities()
    .CreateObjectSet<Foo>("FooView")
    .First();

You can give yourself a headstart by doing the following: 您可以通过执行以下操作为自己做一个开始:

  1. Add Foo and FooView to your model FooFooView添加到您的模型中
  2. In the Mapping Details of Foo click Add a Table or View and select FooView FooMapping Details ,单击Add a Table or View并选择FooView
  3. Delete FooView from your model 从模型中删除FooView
  4. Save the model and open it in the XML editor 保存模型并在XML编辑器中打开它
  5. ( pre-RTM ) Find <EntityType Name="FooView"> in <StorageModels> and remove any incorrect entries from <Key> (it should match <EntityType Name="Foo"> ) RTM之前<StorageModels>找到<EntityType Name="FooView">并从<Key>删除任何不正确的条目(它应匹配<EntityType Name="Foo">
  6. Remove the <EntityTypeMapping Name="IsTypeOf(Foo)" /> and <EntityTypeMapping Name="IsTypeOf(FooView)" /> (they caused me errors) 删除<EntityTypeMapping Name="IsTypeOf(Foo)" /><EntityTypeMapping Name="IsTypeOf(FooView)" /> (它们导致我的错误)

As of beta 2, implementing the above will break the designer 从beta 2开始,实现上述功能将打破设计师的角色

In NHibernate, one should solve this using Projections. 在NHibernate中,应该使用Projections来解决这个问题。 So, I think that there must exists something similar like that in the Entity Framework. 所以,我认为必须存在与实体框架类似的东西。 I've googled a bit, and I came accross this: 我用Google搜索了一下,然后我就来了:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM