Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未在数据库中创建具有映射的表_C#_Sql Server 2008_Entity Framework_Entity Framework 4.1_Entity Framework 5 - Fatal编程技术网

C# 未在数据库中创建具有映射的表

C# 未在数据库中创建具有映射的表,c#,sql-server-2008,entity-framework,entity-framework-4.1,entity-framework-5,C#,Sql Server 2008,Entity Framework,Entity Framework 4.1,Entity Framework 5,我一直在尝试使用EntityFramework5实现存储库模式。我已经有了工作的基础。只有在必要时才会加载我的存储库,并且数据库中的数据会被插入/更新/检索。 问题是:我无法正确创建映射表,甚至根本无法创建映射表。 我一直在网上阅读一些文章,甚至在这里,讨论如何使用映射表并在数据库中创建它们,但我没有成功。把我的问题递过来 我如何让它在我的场景中工作 我将解释到目前为止我在做什么: 下面的方法被重写,据推测,用于创建映射和表 PS:泛型。InvokeGenericMethod是我创建的一个方法,

我一直在尝试使用EntityFramework5实现存储库模式。我已经有了工作的基础。只有在必要时才会加载我的存储库,并且数据库中的数据会被插入/更新/检索。 问题是:我无法正确创建映射表,甚至根本无法创建映射表。 我一直在网上阅读一些文章,甚至在这里,讨论如何使用映射表并在数据库中创建它们,但我没有成功。把我的问题递过来

我如何让它在我的场景中工作

我将解释到目前为止我在做什么:

下面的方法被重写,据推测,用于创建映射和表

PS:
泛型。InvokeGenericMethod
是我创建的一个方法,它正在工作。它照它说的做:)

不工作的部分是我使用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);
    }
堆栈跟踪:

   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)
at System.Collections.Generic.Dictionary`2.FindEntry(TKey)
在System.Collections.Generic.Dictionary`2.TryGetValue(TKey、TValue和value)
在System.Data.Entity.ModelConfiguration.Configuration.Mapping.SortedEntityTypeIndex.Add(EdmEntitySet entitySet,EdmEntityType entityType)
在System.Data.Entity.ModelConfiguration.Configuration.Mapping.EntityMappingService.Analyze()中
在System.Data.Entity.ModelConfiguration.Configuration.Mapping.EntityMappingService.Configure()中
位于System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntityTypes(DbDatabaseMapping databaseMapping,DbProviderManifest providerManifest)
位于System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping,DbProviderManifest providerManifest)
位于System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest,DbProviderInfo providerInfo)
位于System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
位于System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
位于System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput输入)
在System.Data.Entity.Internal.LazyInternalContext.InitializeContext()中
位于System.Data.Entity.Internal.InternalContext.Initialize()处
在System.Data.Entity.Database.Initialize处(布尔强制)
在E:\Gaia\Gaia.Repository\GaiaContext.cs中的Gaia.Repository.GaiaContext..ctor(GaiaContextConfiguration配置)处:第37行
在G:\Gaia\Hermes\HMail.cs中的Hermes.HMail.SendMessage(Int64-sender,Int64[]touser,字符串主题,字符串内容,文件流附件)中:第78行
在G:\Gaia\Gaia\Controllers\ApplicationController.cs中的Gaia.Controllers.ApplicationController.Test()处:第18行
在lambda_方法中(闭包、控制器基、对象[])
在System.Web.Mvc.ActionMethodDispatcher.c__DisplayClass1.b__0中(ControllerBase controller,Object[]参数)
位于System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object[]参数)
位于System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext ControllerContext,IDictionary`2参数)
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor,IDictionary`2参数)
在System.Web.Mvc.ControllerActionInvoker.c_uuDisplayClass15.b_uuu12()中
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter筛选器、ActionExecutingContext预文本、Func`1 continuation)
有什么想法吗


谢谢。

这将是我在stackoverflow中的第一个答案,所以我希望它能帮助别人!事实证明,EF是一种敏感的生物,它不喜欢具有名为“Type”属性的实体。因此,如果您有任何实体,只需将它们重命名为与“EntityType”类似的名称即可

我还听说有人对抽象类型的属性有类似的问题,如果某人的问题不是我上面描述的问题,请查看您的属性类型并检查它们是否是抽象的:
这将是我在stackoverflow中的第一个答案,所以我希望它能帮助别人!事实证明,EF是一种敏感的生物,它不喜欢具有名为“Type”属性的实体。因此,如果您有任何实体,只需将它们重命名为与“EntityType”类似的名称即可

我还听说有人对抽象类型的属性有类似的问题,如果某人的问题不是我上面描述的问题,请查看您的属性类型并检查它们是否是抽象的:

首先,我在第57页中指出,任何内嵌配置都应该仅在所有配置添加到modelbuilder之后出现。检查您的代码

接下来,尝试此版本的代码,以便自动向modelbuilder添加映射。 目标是删除EntityTypeConfiguration中的调用,并使用向上下文添加配置的纯方法。将代码从SetConfiguration移动到构造函数,并尝试使用我的版本。这对我也很有效

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     var entityMapTypes = assembly.GetTypes().Where(
         (t => t.BaseType != null && t.BaseType.IsGenericType && 
               t.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>)));

     foreach (var mapType in entityMapTypes)
     {
         dynamic configurationInstance = Activator.CreateInstance(mapType);
         modelBuilder.Configurations.Add(configurationInstance);
     }
} 
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
var entityMapTypes=assembly.GetTypes()。其中(
(t=>t.BaseType!=null&&t.BaseType.IsGenericType&&
t、 BaseType.GetGenericTypeDefinition()==typeof(EntityTypeConfiguration));
foreach(entityMapTypes中的var mapType)
{
dynamic configurationInstance=Activator.CreateInstance(mapType);
modelBuilder.Configurations.Add(configurationInstance);
}
} 

首先,我在第57页中发现,任何在线配置,
   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)
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     var entityMapTypes = assembly.GetTypes().Where(
         (t => t.BaseType != null && t.BaseType.IsGenericType && 
               t.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>)));

     foreach (var mapType in entityMapTypes)
     {
         dynamic configurationInstance = Activator.CreateInstance(mapType);
         modelBuilder.Configurations.Add(configurationInstance);
     }
}