【发布时间】:2012-04-14 13:07:08
【问题描述】:
我一直在尝试使用 EntityFramework5 实现存储库模式。我已经掌握了工作的基础。仅在必要时才加载我的存储库,并且数据会按应有的方式在数据库中插入/更新/检索。 问题是:我无法正确创建带有映射的表,甚至根本无法创建。 我一直在网上阅读一些文章,甚至在这里,谈论使用映射表并在数据库中创建它们的方法,但我没有成功。在这里提出我的问题。
如何让它在我的场景中工作?
我将解释到目前为止我在做什么:
下面的这个方法被覆盖,据说是创建映射和表。
PS:Generics.InvokeGenericMethod 是我创建的一种方法,它正在工作。它按照它说的做:)
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
foreach (Type entityType in Repositories.Keys)
{
//Invokes the Entity method from DbModelBuilder injecting the class type.
Generics.InvokeGenericMethod(modelBuilder, typeof(DbModelBuilder), entityType, "Entity", null);
}
foreach (GaiaEntityConfiguration config in EntityConfigurations)
{
//Defines each mapping existing in the context
config.SetConfiguration(modelBuilder);
}
}
不工作的部分是我使用 SetConfiguration 的地方。 SetConfiguration 是我为每个添加到具有映射的上下文的类创建的方法,将该映射镜像到数据库。
这是一个示例(在这种情况下,一个收件人有很多消息,而消息有很多收件人/多对多关联):
PS:我留下了注释代码,以便您可以看到我尝试的另一种方法。
public RecipientEntityConfiguration()
{
this.LeftKey = "RecipientId";
this.RightKey = "MessageId";
this.Schema = "Hermes";
this.TableName = "RecipientMessage";
}
public override void SetConfiguration(DbModelBuilder modelBuilder)
{
//EntityTypeConfiguration<Recipient> entityTypeConfiguration = new EntityTypeConfiguration<Recipient>();
//entityTypeConfiguration
// .HasMany<Message>(r => r.Messages)
// .WithMany(m => m.Recipients)
// .Map(mr =>
// {
// mr.MapLeftKey(this.LeftKey);
// mr.MapRightKey(this.RightKey);
// mr.ToTable(this.TableName, this.Schema);
// });
modelBuilder.Entity<Recipient>()
.HasMany<Message>(r => r.Messages)
.WithMany(m => m.Recipients)
.Map(mr =>
{
mr.MapLeftKey(this.LeftKey);
mr.MapRightKey(this.RightKey);
mr.ToTable(this.TableName, this.Schema);
});
//modelBuilder.Configurations.Add<Recipient>(entityTypeConfiguration);
}
当this.Database.Initialize(false); 被调用时,我得到这个错误:
Value cannot be null.
Parameter name: key
堆栈跟踪:
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
at System.Data.Entity.ModelConfiguration.Configuration.Mapping.SortedEntityTypeIndex.Add(EdmEntitySet entitySet, EdmEntityType entityType)
at System.Data.Entity.ModelConfiguration.Configuration.Mapping.EntityMappingService.Analyze()
at System.Data.Entity.ModelConfiguration.Configuration.Mapping.EntityMappingService.Configure()
at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntityTypes(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Database.Initialize(Boolean force)
at Gaia.Repository.GaiaContext..ctor(GaiaContextConfiguration config) in E:\Gaia\Gaia.Repository\GaiaContext.cs:line 37
at Hermes.HMail.SendMessage(Int64 sender, Int64[] toUsers, String subject, String content, FileStream attachment) in G:\Gaia\Hermes\HMail.cs:line 78
at Gaia.Controllers.ApplicationController.Test() in G:\Gaia\Gaia\Controllers\ApplicationController.cs:line 18
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.<>c__DisplayClass1.<WrapVoidAction>b__0(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
有什么想法吗?
谢谢。
【问题讨论】:
-
很难像这样遵循,因为您对 EF/CF 的使用更复杂(通常不这样做,所以如果可行,您可能会遇到问题)。您能否发布一个有效的演示/重现,这会有所帮助,否则我们只能猜测。
-
@NSGaga 我明白了。这就是为什么我试图详细解释一切并说出什么是有效的。并回答您的问题:工作部分已经存在......当我调用每个表的实体方法时。唯一的问题是我在其中设置配置的 foreach。如果我删除它,则会创建表,但没有映射表。如果您需要更多代码,请告诉我是哪一部分。谢谢。
-
我明白,最好能在本地快速运行它,然后看看发生了什么 - 否则它有点“抽象”而且它是一个不寻常的用途 - 尽管你可以放弃它:)。我用自定义 ORM 做了类似的事情。简而言之,我需要“整件事”:) 只是缩小到一个类/模型,并且只为它设置一个配置模型构建器 - 可以编译(尽可能多地)并产生错误的东西。
-
@NSGaga 我明白了,这可能有点难。到目前为止,我有 10 个项目img52.imageshack.us/img52/8821/ashampoosnap2012041410h.png,即使我正在做这个项目是为了学习,我也有一些敏感信息通过代码传播。无论哪种方式,我仍然试图自己弄清楚。如果我不能,我想我只能等待。再次感谢您的宝贵时间。
-
明白了——但你可以删掉“技术相关”部分——即为什么不只添加一个实体(测试)和你的通用调用——然后配置问题,“解决这个问题' - 我不期待完整的代码 :) 不,请,只有几个衬里。它也可以帮助您更好地理解这一点。 np
标签: c# sql-server-2008 entity-framework entity-framework-4.1 entity-framework-5