简体   繁体   English

如何使用EF6 Fluent API for MySQL指定表类型?

[英]How to specify a table type using EF6 Fluent API for MySQL?

So I am using EF6's Fluent API to specify entity properties in my code first application. 所以我使用EF6的Fluent API在我的代码第一个应用程序中指定实体属性。 However I would like to specify a table type for one of my entities, is it possible to do this via the api? 但是我想为我的一个实体指定一个表类型,是否可以通过api执行此操作?

ApplicationDbContext: ApplicationDbContext:

 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

So lets say I wanted to set the ApplicationUser table to use the MyISAM table engine, how can I do this? 所以我想说我想将ApplicationUser表设置为使用MyISAM表引擎,我该怎么做呢?

You cannot do that .B'cos you can use Fluent API to configure your domain classes.But fluent API doesn't know anything about the specific database provider's details.It is a generic API to map your POCO classes to tables and its columns . 不能这样做 .B'cos你可以使用Fluent API来配置你的域类。但是fluent API对特定数据库提供者的详细信息一无所知。它是一个通用的API,用于将POCO类映射到tables及其columns

MYSQL is a relational database provider and it's having lot of provider specific components.As an example it's having different table engines. MYSQL是一个关系数据库提供者,它拥有许多特定于提供者的组件。例如,它有不同的表引擎。 such as InnoDB , MyISAM and etc.But Fluent Api is just a mapper which doesn't know anything about the database provider's specific implementations. 比如InnoDBMyISAM等。但是Fluent Api只是一个映射器,对数据库提供者的具体实现一无所知。

为此,您需要创建一个迁移,然后在Up方法中编写自己的Sql命令,如下所示:

Sql("DROP TABLE Badger");

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM