[英]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.