声明: 我知道还有其他问题,标题与我的相似。但是,其他人没有使用EF7 RC1,pm命令也不同。
我的问题是无论我做什么,我都无法让EF7创建我的桌子。此外,我无法强制执行任何操作或-ignorechanges甚至-script,因为这些选项似乎已从命令中删除。
我的背景是
public class EventsContext : DbContext//ContextBase
{
public DbSet<Publisher> Publishers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings[Constants.Database.CONTEXT_NAME].ConnectionString);
}
protected override void OnModelCreating(ModelBuilder builder)
{
const string entityName = "Publisher";
builder.HasSequence<long>(entityName).StartsAt(1).IncrementsBy(1);
builder.Entity<Publisher>(o =>
{
o.HasKey(x => x.Id);
o.Property(x => x.Id).HasDefaultValueSql($"NEXT VALUE FOR {entityName}");
o.Property(x => x.LoggedUserId).IsRequired();
o.Property(x => x.CreatedDate).IsRequired().HasDefaultValue(DateTime.SpecifyKind(DateTime.UtcNow, DateTimeKind.Utc));
o.Property(x => x.UpdatedDate).IsRequired();
o.Property(x => x.Deleted).IsRequired().HasDefaultValue(false);
o.Property(x => x.RowVersion).ValueGeneratedOnAddOrUpdate().IsConcurrencyToken();
o.ToTable(Constants.Database.TableNames.PUBLISHER);
o.Property(x => x.Name).HasMaxLength(Constants.DataFields.Length.MEDIUM_TEXT);
o.Property(x => x.Email).HasMaxLength(Constants.DataFields.Length.LONG_TEXT);
o.Property(x => x.FacebookAccount).HasMaxLength(Constants.DataFields.Length.LONG_TEXT);
o.Property(x => x.TwitterAccount).HasMaxLength(Constants.DataFields.Length.LONG_TEXT);
});
builder.Ignore<Event>();
}
}
我只是在玩序列和rowversion。
我的发布商类是
public class Publisher
{
public virtual long LoggedUserId { get; set; }
public virtual DateTime CreatedDate { get; set; }
public virtual DateTime UpdatedDate { get; set; }
public virtual byte[] RowVersion { get; set; }
public virtual long Id { get; protected set; }
public virtual bool Deleted { get; set; }
public virtual string FacebookAccount { get; set; }
public virtual string TwitterAccount { get; set; }
public virtual string Email { get; set; }
public virtual string Name { get; set; }
public virtual IList<Event> Events { get; set; }
}
之后
add-migration init
我得到了
public partial class init : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateSequence(
name: "Publisher");
migrationBuilder.CreateTable(
name: "Publisher",
columns: table => new
{
Id = table.Column<long>(nullable: false, defaultValueSql: "NEXT VALUE FOR Publisher"),
CreatedDate = table.Column<DateTime>(nullable: false, defaultValue: new DateTime(2016, 2, 24, 14, 50, 56, 456, DateTimeKind.Unspecified)),
Deleted = table.Column<bool>(nullable: false, defaultValue: false),
Email = table.Column<string>(nullable: true),
FacebookAccount = table.Column<string>(nullable: true),
LoggedUserId = table.Column<long>(nullable: false),
Name = table.Column<string>(nullable: true),
RowVersion = table.Column<byte[]>(nullable: true),
TwitterAccount = table.Column<string>(nullable: true),
UpdatedDate = table.Column<DateTime>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Publisher", x => x.Id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropSequence("Publisher");
migrationBuilder.DropTable("Publisher");
}
}
之后
update-database
我得到了
应用迁移'20160224145056_init'。 System.Data.SqlClient.SqlException(0x80131904):已经有了 数据库中名为“Publisher”的对象。在 System.Data.SqlClient.SqlConnection.OnError(SqlException异常, 布尔值breakConnection,Action
1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction)at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler,SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,布尔&amp; dataReady)at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(字符串 methodName,Boolean async,Int32 timeout,Boolean asyncWrite)at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Microsoft.Data.Entity.Storage.Internal.RelationalCommand.<>c.<ExecuteNonQuery>b__13_0(DbCommand cmd, IRelationalConnection con) at Microsoft.Data.Entity.Storage.Internal.RelationalCommand.Execute[T](IRelationalConnection connection, Func
3 action,String executeMethod,Boolean openConnection,Boolean closeConnection)at Microsoft.Data.Entity.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection,Boolean manageConnection)at Microsoft.Data.Entity.Storage.RelationalCommandExtensions.ExecuteNonQuery(IEnumerable的1 commands, IRelationalConnection connection) at Microsoft.Data.Entity.Migrations.Internal.Migrator.Execute(IEnumerable
1 relationalCommands)at Microsoft.Data.Entity.Migrations.Internal.Migrator.Migrate(字符串 targetMigration) Microsoft.Data.Entity.Design.MigrationsOperations.UpdateDatabase(字符串 targetMigration,String contextType)at Microsoft.Data.Entity.Design.OperationExecutor.UpdateDatabaseImpl(字符串 targetMigration,String contextType)at Microsoft.Data.Entity.Design.OperationExecutor.UpdateDatabase&LT;&GT; c__DisplayClass0_1&LT; .ctor&GT; b__0() 在 Microsoft.Data.Entity.Design.OperationExecutor.OperationBase.Execute(动作 action)ClientConnectionId:c34ffd41-714b-4412-a20f-8e18a58b58c1错误 编号:2714,状态:6,类:16已经有一个名为的对象 数据库中的“发布者”。
答案 0 :(得分:0)
事实证明我在数据库上有重复的名称。序列和表格具有相同的名称。