gpt4 book ai didi

c# - 无法确定导航属性 ASP.NET core 2.0 Entity Framework 表示的关系

转载 作者:太空狗 更新时间:2023-10-29 17:45:45 26 4
gpt4 key购买 nike

我目前正在为给定代码开发一个使用 EntityFramework 的 ASP.NET 应用

public partial class EnumMeta : Meta
{
public EnumMeta()
{
this.Messages = new HashSet<MessageMeta>();
}

public bool IsFlag { get; set; }

public virtual ICollection<MessageMeta> Messages { get; set; }
}

public partial class MessageMeta : Meta
{
public MessageMeta()
{
this.Enums = new HashSet<EnumMeta>();
this.Receiver = new HashSet<Module>();
this.Sender = new HashSet<Module>();
}

public int MsgId { get; set; }

public virtual ICollection<EnumMeta> Enums { get; set; }
public virtual ICollection<Module> Receiver { get; set; }
public virtual ICollection<Module> Sender { get; set; }
}

我收到一个错误:

Unable to determine the relationship represented by navigation property 'EnumMeta.Messages' of type 'ICollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

此代码由 EntityFramework 使用 WPF 项目自动生成,然后将模型带到 ASP.NET core 2.0 项目以对数据库执行“代码优先”方法。

我已经尝试设置 [ForeignKey("MessageMeta")]以上public virtual ICollection<MessageMeta> Messages { get; set; }没有正解。

最佳答案

正如我之前评论的那样,Entity FrameworkEntity Framework Core 2.0 之间存在许多差异,我认为您不能只采用从 生成的模型code>Entity Framework 并使用 Entity Framework Core 2.0 运行 Code First。

从描述的模型来看,MessageMetaEnumMeta 似乎是多对多的关系。

直到今天,Entity Framework Core 2.0 仍然不支持中间类的多对多。因此,为了在 EF Core 2.0 中执行多对多操作,您需要中间类。

消息元

using System.Collections.Generic;

namespace DL.SO.MessageEnum.Web.UI.Models
{
public class MessageMeta
{
public int MsgId { get; set; }

public List<MessageEnum> EnumMetas { get; set; }
}
}

枚举元

using System.Collections.Generic;

namespace DL.SO.MessageEnum.Web.UI.Models
{
public class EnumMeta
{
public int EnumId { get; set; }
public bool IsFlag { get; set; }

public List<MessageEnum> MessageMetas { get; set; }
}
}

MessageEnum(或者你可以称它为 EnumMessage)——中间模型

namespace DL.SO.MessageEnum.Web.UI.Models
{
public class MessageEnum
{
public int MessageMetaId { get; set; }
public MessageMeta MessageMeta { get; set; }

public int EnumMetaId { get; set; }
public EnumMeta EnumMeta { get; set; }
}
}

数据库上下文

using Microsoft.EntityFrameworkCore;

namespace DL.SO.MessageEnum.Web.UI.Models
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options): base(options) { }

public DbSet<MessageMeta> Messages { get; set; }
public DbSet<EnumMeta> Enums { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MessageMeta>()
.HasKey(x => x.MsgId);

modelBuilder.Entity<EnumMeta>()
.HasKey(x => x.EnumId);

modelBuilder.Entity<MessageEnum>()
.HasKey(x => new { x.MessageMetaId, x.EnumMetaId });
modelBuilder.Entity<MessageEnum>()
.HasOne(x => x.MessageMeta)
.WithMany(m => m.EnumMetas)
.HasForeignKey(x => x.MessageMetaId);
modelBuilder.Entity<MessageEnum>()
.HasOne(x => x.EnumMeta)
.WithMany(e => e.MessageMetas)
.HasForeignKey(x => x.EnumMetaId);
}
}
}

应用设置.json

{
"ConnectionStrings": {
"AppDbConnection": "Data Source=.\\SQLEXPRESS;Initial Catalog=SO.MessageEnum;Integrated Security=True;MultipleActiveResultSets=False;"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}

启动

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using DL.SO.MessageEnum.Web.UI.Models;
using Microsoft.EntityFrameworkCore;

namespace DL.SO.MessageEnum.Web.UI
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
string dbConnectionString = Configuration.GetConnectionString("AppDbConnection");

services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(dbConnectionString)
);

services.AddMvc();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}

app.UseStaticFiles();

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}

要在包管理器控制台上运行的命令

Add-Migration Init
Update-Database

生成的数据库

enter image description here

关于c# - 无法确定导航属性 ASP.NET core 2.0 Entity Framework 表示的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46839391/

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