2016-09-23 30 views
1

因此,我使用EF6的Fluent API在我的代碼第一個應用程序中指定實體屬性。不過,我想爲我的一個實體指定一個表類型,是否可以通過api執行此操作?如何使用EF6 Fluent API爲MySQL指定表類型?

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 

因此,可以說,我想設置ApplicationUser表使用MyISAM臺引擎,我該怎麼辦呢?

+0

你是什麼'engine'是什麼意思? – Sampath

+0

MySQL有多個表引擎會影響表的工作方式和目的。例如,InnoDB是默認的,另一個是Archive表引擎,它基本上只是以性能和其他關鍵表特性爲代價來壓縮內存 –

回答

0

要做到這一點,你需要創建一個遷移,然後編寫自己的SQL在向上方法命令,像這樣:

Sql("DROP TABLE Badger"); 
0

不能這樣做 .B'cos您可以使用Fluent API來配置你的域classes.But fluent API不知道具體的數據庫提供商的details.It什麼是一個通用的API,將POCO類映射到tables和它的columns

MYSQL是一個關係數據庫提供程序,它有很多提供程序特定組件。例如它有不同的表引擎。如InnoDBMyISAM等。但流利的API只是一個映射器,它不知道任何有關數據庫提供者的具體實現。

相關問題