C# 如何使用EF6 Fluent API for MySQL指定表类型?
因此,我使用EF6的Fluent API在我的代码第一个应用程序中指定实体属性。但是,我想为我的一个实体指定一个表类型,是否可以通过api来实现这一点 ApplicationDbContext:C# 如何使用EF6 Fluent API for MySQL指定表类型?,c#,mysql,entity-framework,asp.net-identity,C#,Mysql,Entity Framework,Asp.net Identity,因此,我使用EF6的Fluent API在我的代码第一个应用程序中指定实体属性。但是,我想为我的一个实体指定一个表类型,是否可以通过api来实现这一点 ApplicationDbContext: public class ApplicationUser : IdentityUser { public string CreatedBy { get; set; } public DateTime Active { get; set; } pu
public class ApplicationUser : IdentityUser
{
public string CreatedBy { get; set; }
public DateTime Active { get; set; }
public DateTime RememberMeTimeOut { get; set; }
public int CentralPermissionUserID { get; set; }
public virtual ICollection<UserLogs> UserLogs { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DBContext", throwIfV1Schema: false)
{
}
public DbSet<UserLogs> UserLogs { get; set; }
public DbSet<Supplier> Supplier { get; set; }
public DbSet<SupplierArchive> SupplierArchive { get; set; }
public DbSet<MenuStatsSummary> MenuStatsSummary { get; set; }
public DbSet<Pot> Pots { get; set; }
public DbSet<PotDupeStandards> PotDupeStandards { get; set; }
public DbSet<DupeStandards> DupeStandards { get; set; }
public DbSet<DupeStandardMapping> DupeStandardMapping { get; set; }
public DbSet<DupeData> DupeData { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
#region Application User
modelBuilder.Entity<ApplicationUser>().Property(u => u.UserName).HasMaxLength(255);
modelBuilder.Entity<ApplicationUser>().Property(u => u.Email).HasMaxLength(255);
modelBuilder.Entity<ApplicationUser>().Property(u => u.CreatedBy).HasMaxLength(50).IsRequired();
modelBuilder.Entity<ApplicationUser>().Property(u => u.CentralPermissionUserID).IsRequired();
modelBuilder.Entity<ApplicationUser>()
.HasMany(u => u.UserLogs)
.WithRequired(u => u.User)
.HasForeignKey(u => u.UserID)
.WillCascadeOnDelete(true);
modelBuilder.Entity<ApplicationUser>()
.Property(u => u.Email)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("UQ_Email") { IsUnique = true }));
modelBuilder.Entity<ApplicationUser>()
.Property(u => u.UserName)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("UQ_UserName") { IsUnique = true }));
#endregion
公共类应用程序用户:IdentityUser
{
通过{get;set;}创建的公共字符串
公共日期时间活动{get;set;}
public DateTime RememberMeTimeOut{get;set;}
公共int CentralPermissionUserID{get;set;}
公共虚拟ICollection用户日志{get;set;}
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
}
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
公共类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“DBContext”,throwifvv1schema:false)
{
}
公共数据库集用户日志{get;set;}
公共数据库集提供程序{get;set;}
公共数据库集供应商存档{get;set;}
公共数据库集菜单USTATSSUMMARY{get;set;}
公共数据库集{get;set;}
公共数据库集PotDupeStandards{get;set;}
公共数据库集双重标准{get;set;}
公共DbSet DupeStandardMapping{get;set;}
公共DbSet DupeData{get;set;}
公共静态应用程序上下文创建()
{
返回新的ApplicationDbContext();
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
#区域应用程序用户
modelBuilder.Entity().Property(u=>u.UserName).HasMaxLength(255);
modelBuilder.Entity().Property(u=>u.Email).HasMaxLength(255);
modelBuilder.Entity().Property(u=>u.CreatedBy).HasMaxLength(50).IsRequired();
modelBuilder.Entity().Property(u=>u.CentralPermissionUserID).IsRequired();
modelBuilder.Entity()
.HasMany(u=>u.userlog)
.WithRequired(u=>u.User)
.HasForeignKey(u=>u.UserID)
.WillCascadeOnDelete(真);
modelBuilder.Entity()
.Property(u=>u.Email)
.HasColumnAnnotation(“索引”,新索引注释(新索引属性(“UQ_电子邮件”){IsUnique=true});
modelBuilder.Entity()
.Property(u=>u.UserName)
.HasColumnAnnotation(“索引”,新索引注释(新索引属性(“UQ_用户名”){IsUnique=true});
#端区
假设我想将
ApplicationUser
表设置为使用MyISAM
表引擎,我该怎么做呢?你做不到这一点。因为你可以使用Fluent API
来配置你的域类。但是Fluent API
对特定数据库提供程序的详细信息一无所知t是一个通用API,用于将POCO
类映射到表及其列
MYSQL
是一个关系数据库提供程序,它有很多特定于提供程序的组件。例如,它有不同的表引擎。例如InnoDB
,MyISAM
等等。但是Fluent Api只是一个映射程序,它不知道数据库提供程序的具体实现。You无法执行此操作。因为您可以使用Fluent API
配置域类。但是Fluent API
不知道特定数据库提供程序的详细信息。它是一种通用API,用于将POCO
类映射到表及其列
MYSQL
是一个关系数据库提供程序,它有很多特定于提供程序的组件。例如,它有不同的表引擎。例如InnoDB
,MyISAM
等等。但是Fluent Api只是一个映射程序,它不知道数据库提供程序的具体实现。为此,请您需要创建迁移,然后在Up方法中编写自己的Sql命令,如下所示:
Sql("DROP TABLE Badger");
为此,您需要创建迁移,然后在Up方法中编写自己的Sql命令,如下所示:
Sql("DROP TABLE Badger");
你说的引擎是什么意思?MySQL有多个表引擎,它们会影响表的工作方式和用途。例如,InnoDB是默认的,另一个是存档表引擎,它基本上只是压缩内存,而牺牲了性能和其他关键表功能。你说的引擎是什么意思表引擎影响表的工作方式和用途。例如InnoDB是默认的,另一个是存档表引擎,它基本上只是压缩内存,而牺牲了性能和其他关键表功能