gpt4 book ai didi

c# - Entity Framework v4 -> 需要一些关于 POCO 和实体的帮助

转载 作者:太空狗 更新时间:2023-10-30 01:27:55 26 4
gpt4 key购买 nike

我正在使用 EF4,我有两个实体,我希望将它们映射到相同 POCO。我不确定我该怎么做。

Entity 1 → Foo (this represents a table FOO in the db)
POCO → Foo

Entity 2 → FooView (this represents a view FooView in the db)
POCO → Foo

我知道我需要做类似的事情

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

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

但是当我尝试这样做时,它确实可以编译,但是失败并出现以下异常:

System.ArgumentException: System.ArgumentException: The specified entity type, 'MyProject.Core.Foo', does not match the type 'EntityFramework.SqlServerModel.FoosView' from the EntitySet 'FoosViews'.

有什么建议吗?

最佳答案

以下是要查找的事项 list :

  1. 您的存储模型应该有:
    1. 两个 EntitySets : FooFooView
    2. 两个 EntityTypes : FooFooView
  2. 您的概念模型应该有:
    1. 两个 EntitySets : FooFooView - 带有EntityType设置为 ModelName.Foo
    2. 一个 EntityType : Foo
  3. 您的映射 应该有两个 EntitySetMappings :
    1. Foo与一个EntityTypeMapping ("ModelName.Foo") 和一个 MappingFragment (“富”)
    2. FooView与一个EntityTypeMapping ("ModelName.Foo") 和一个 MappingFragment ("FooView")

您应该能够执行以下操作:

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

您可以通过执行以下操作让自己抢先一步:

  1. 添加FooFooView给你的模特
  2. Mapping DetailsFoo点击 Add a Table or View并选择 FooView
  3. 删除FooView来自你的模型
  4. 保存模型并在 XML 编辑器中打开它
  5. ( pre-RTM ) 查找 <EntityType Name="FooView"><StorageModels>并从 <Key> 中删除所有不正确的条目(它应该匹配 <EntityType Name="Foo"> )
  6. 删除 <EntityTypeMapping Name="IsTypeOf(Foo)" /><EntityTypeMapping Name="IsTypeOf(FooView)" /> (他们让我犯了错误)

从 beta 2 开始,实现上述内容会破坏设计器

关于c# - Entity Framework v4 -> 需要一些关于 POCO 和实体的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1993988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com