【问题标题】:Working with Fluent API使用 Fluent API
【发布时间】:2014-09-12 17:54:48
【问题描述】:

以下是我的数据库上下文,我已经覆盖了名为OnModelCreating 的方法以使用Fluent API。当我构建解决方案时,我没有收到任何错误,但架构也保持不变。

public class BreakAwayContext : DbContext
    {
        public BreakAwayContext()
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<BreakAwayContext>());
        }
        public DbSet<Destination> Destinations { get; set; }
        public DbSet<Lodging> Lodgings { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Destination>().Property(d => d.Name).IsRequired();
            modelBuilder.Entity<Destination>().Property(d => d.Description).HasMaxLength(600);
            modelBuilder.Entity<Destination>().Property(d => d.Photo).HasColumnType("image");
            modelBuilder.Entity<Lodging>().Property(l => l.Name).IsRequired().HasMaxLength(200);
        }

    }

这是一个屏幕截图,显示 Description 列的长度没有变化。

覆盖上述方法后是否需要Add-MigrationUpdate-Database

【问题讨论】:

  • 添加模型创建后是否使用过db上下文?
  • 如你所说,尝试更新数据库。

标签: c# asp.net asp.net-mvc entity-framework entity-framework-4


【解决方案1】:

问题已解决。我必须使用Add-Migration,然后是Update-Database

【讨论】:

    猜你喜欢
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 2014-12-31
    • 2016-03-06
    相关资源
    最近更新 更多