简体   繁体   English

实体框架具有多个MySql模式的多个DbContext

[英]Entity Framework Multiple DbContexts With Multiple MySql Schemas

I am trying to implement two DbContexts which map to two seperate schemas/databases within MySql with a foreign key between them. 我正在尝试实现两个DbContext,这些DbContext映射到MySql中的两个独立的架构/数据库,并且它们之间具有外键。

I understand questions similar to this have been asked before, but I can't find an answer in relation to MySql 我知道以前曾问过类似的问题,但找不到与MySql有关的答案

I am using code first but and I'm getting the following error when I do Update-Database: 我先使用代码,但是在执行Update-Database时出现以下错误:

MultipleDbContext.ApplicationUser: : EntityType 'ApplicationUser' has no key defined. MultipleDbContext.ApplicationUser::EntityType'ApplicationUser'未定义键。 Define the key for this EntityType. 定义此EntityType的键。

ApplicationUsers: EntityType: EntitySet 'ApplicationUsers' is based on type 'ApplicationUser' that has no keys defined. ApplicationUsers:EntityType:EntitySet'ApplicationUsers'基于未定义键的'ApplicationUser'类型。

These are my 2 DbContexts: 这些是我的2个DbContext:

ApplicationDbContext ApplicationDbContext

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext() : base("ApplicationDBContext") {}

    public DbSet<Application> Applications { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Configurations.Add(new ApplicationConfiguration());
    }
}

public class ApplicationConfiguration : EntityTypeConfiguration<Application>
{
    public ApplicationConfiguration()
    {
        HasKey(x => x.ApplicationID);
        Property(x => x.ApplicationID).IsRequired();
        Property(x => x.ApplicationName).IsRequired();
        HasRequired(x => x.PrimaryUser).WithMany().HasForeignKey(x => x.UserID);
    }
}

ApplicationUserDbContext ApplicationUserDbContext

public class ApplicationUserDbContext : DbContext
{
    public ApplicationUserDbContext() : base("UserDBContext") {}

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new ApplicationUserConfiguration());
    }
}

public class ApplicationUserConfiguration : EntityTypeConfiguration<ApplicationUser>
{
    public DbSet<ApplicationUser> ApplicationUsers { get; set; }

    public ApplicationUserConfiguration()
    {
        HasKey(x => x.UserID);
        Property(x => x.UserID).IsRequired();
        Property(x => x.Name).IsRequired();
    }
}

This is my update database statement: 这是我的更新数据库语句:

update-database -ConfigurationTypeName MultipleDbContext.Migrations.Configuration 更新数据库-ConfigurationTypeName MultipleDbContext.Migrations.Configuration

Thanks! 谢谢!

EDIT - Adding Entity Objects 编辑-添加实体对象

public class Application
{
    public int ApplicationID { get; set; }
    public string ApplicationName { get; set; }
    public int UserID { get; set; }
    public virtual ApplicationUser PrimaryUser { get; set; }
}

public class ApplicationUser
{
    public int UserID { get; set; }
    public string Name { get; set; }
}

Problem probably is, that you have string property as a the primary key for your User and the length of this key is too long for MySql. 问题可能是,您具有字符串属性作为用户的主键,并且此键的长度对于MySql而言太长。 I believe the limitation is 96 characters for keys (767 bytes). 我相信密钥的限制是96个字符(767字节)。

A way to handle this would be to take only a subset of the string property and apply this to the key. 处理此问题的一种方法是仅获取字符串属性的子集,并将其应用于键。 To make the point clear, the following shows a key, which is only 4 chars long. 为了清楚说明这一点,下面显示了一个只有4个字符的密钥。

CREATE TABLE IF NOT EXISTS `foo` (
  `id` varchar(128),
  PRIMARY KEY (`id`(4)),
)

I'd suggest going all in with integer primary keys for the Aspnet.Identity stack - and also make tablenames all lowercase, since this would be an issue on mysql server hosted via case-sensitive filesystems. 我建议全部使用Aspnet.Identity堆栈的整数主键-并使表名全部小写,因为这是通过区分大小写的文件系统托管的mysql服务器上的问题。

This GitHub repo is kinda overkill as an example, but im pointing out a few points in the code here 这个GitHub回购示例有点过分,但是我在这里指出了代码中的几点

Also this answer has a nice walkthrough 而且这个答案有一个很好的演练

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架和多个模式 - Entity Framework and multiple schemas Entity Framework Core 使用多个 DbContext - Entity Framework Core Using multiple DbContexts 实体框架核心:创建合并多个DbContexts的迁移(具有多个迁移) - Entity Framework Core: create a migration that merges multiple DbContexts (with multiple migrations) 使用Entity Framework 6的多个上下文,跨dbcontexts引用了多个实体 - Multiple contexts with Entity Framework 6, reference entities across dbcontexts 集成测试共享数据库的多个Entity框架dbcontexts - Integration testing multiple Entity framework dbcontexts that share a database dotnet Core - 实体框架 - 多个 DbContext 选择错误的 DbContext - dotnet Core - Entity Framework - Multiple DbContexts choosing wrong DbContext 实体框架和在单个上下文中具有多个模式的迁移 - Entity Framework and Migration with multiple schemas in a single Context 附加到多个DbContext的实体的参照相等性? - Referential equality of entity attached to multiple DbContexts? 实体框架,具有从同一数据库中的基本DbContexts进行的多次迁移和继承 - Entity Framework with multiple migrations and inheritance from base DbContexts within the same Database Entity Framework 4 从具有多个模式的模型生成数据库 - Entity Framework 4 Generate Database From Model With Multiple Schemas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM