没有在数据库中创建映射的表 [英] Tables with mapping not being created at the database

查看:57
本文介绍了没有在数据库中创建映射的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用EntityFramework5实现Repository模式。我已经掌握了基础知识。我的存储库只在必要时加载,并且数据库正在数据库中插入/更新/检索。

问题是:我无法获得正确创建映射的表或者甚至创建了。

我一直在网上阅读一些文章,甚至在这里,谈论使用映射表并在数据库中创建它们的方法,但我没有成功。在这里提出我的问题。



如何在我的方案中使用它?
b

我ll解释我到目前为止所做的事情:



下面这个方法被覆盖,据说可以创建映射和表格。



PS: Generics.InvokeGenericMethod 是我创建的方法,它正在运行。它做的就是它所说的:)



I've been trying to implement the Repository pattern using EntityFramework5. I already got the basics to work. My repositories are being loaded only when necessary and the data is being inserted/updated/retrieved in the database as it should.
The problem is: I can't get the tables with mapping to be correctly created, or even created at all.
I've been reading some articles online, even here, talking about ways to work with mapped tables and create them in the database, but I haven't succeeded. Hands my question here.

How do I get it to work in my scenario?

I'll explain what I'm doing so far:

This method below is overriden to, supposedly, create the mappings and tables.

PS: Generics.InvokeGenericMethod is a method I created and it's working. It does what it says :)

        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:我留下了注释代码,所以你可以看到我试过的另一种方法。



$


    




The part that is not working is where I use SetConfiguration. SetConfiguration is a method I created for each class added to the context that has mapping, have that mapping mirrored to the database.

Here's one example (in this case a recipient has many messages and messages have many recipients / many-to-many association):

PS: I left the commented code so you can see one other approach I tried.



        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 ); 被称为我收到此错误:






When this.Database.Initialize(false); is called I get this error:

    Value cannot be null.
    Parameter name: key

调用ObjectContext时会发生这种情况。



StackTrace:



It happens when ObjectContext is called.

StackTrace:

       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)








有任何想法吗?



谢谢。






Any ideas?

Thanks.

推荐答案

您好,

阅读以下主题。它可能对你有帮助。

Read the thread below. It may help you.

http://stackoverflow.com/questions/257772/structuremap-error-when-invalid-controller

http://stackoverflow.com/questions/257772/structuremap-error-when-invalid-controller


这篇关于没有在数据库中创建映射的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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