当我试图通过EF迁移使我的数据库版本化时,我收到错误"从字符串转换日期和/或时间时转换失败".问题是我的MS Sql Server版本(带有高级服务的Microsoft SQL Server Express Edition)不支持EF生成的日期字符串('2012-03-21T18:23:13.525Z')用于新的迁移脚本条目(64-位)).这是一个错误吗?有工作吗?
CREATE TABLE [__MigrationHistory] (
[MigrationId] [nvarchar](255) NOT NULL,
[CreatedOn] [datetime] NOT NULL,
[Model] [varbinary](max) NOT NULL,
[ProductVersion] [nvarchar](32) NOT NULL,
CONSTRAINT [PK___MigrationHistory] PRIMARY KEY ([MigrationId])
)
BEGIN TRY
EXEC sp_MS_marksystemobject '__MigrationHistory'
END TRY
BEGIN CATCH
END CATCH
INSERT INTO [__MigrationHistory] ([MigrationId], [CreatedOn], [Model], [ProductVersion])
VALUES ('201203210144184_init', '2012-03-21T18:23:13.525Z', 0x33, '4.3.1');
Run Code Online (Sandbox Code Playgroud)
编辑
没关系.这与EntityFramework无关.如果我的SQL Server数据库配置为向后兼容SQL Server 2000,它将不接受该日期格式.我想除非我可以让EF以另一种格式输出它的日期字符串(或者让SQL Server向后兼容到2000并仍然理解EF日期字符串),我将无法在我的数据库中使用EF迁移: - (.如果有人找到了将EF与兼容级别为SQL Server 2000的数据库一起使用的方法,请告诉我.
sql-server-2000 backwards-compatibility ef-migrations entity-framework-4.3
在我的项目中,我首先使用WebSecurity和EF代码进行迁移.
我有我想要与WebSecurity结合的自定义UserProfile类.
我想在Seed方法的迁移'Configuration类中为用户播种.
所以我试试这个:
#1)
if (!Roles.RoleExists("admin"))
Roles.CreateRole("admin");
if (!WebSecurity.UserExists(AdminUserName))
WebSecurity.CreateUserAndAccount(
AdminUserName,
"admin",
new {Email = "admin@mindmap.pl"});
Run Code Online (Sandbox Code Playgroud)
但它抱怨我应该首先调用WebSecurity.InitializeDatabaseConnection.
好的,这是有道理的.所以我在Global.asax中添加了以下行:
#2)
WebSecurity.InitializeDatabaseConnection(
_connectionStringName,
"UserProfiles",
"Id",
"UserName",
autoCreateTables: true);
Run Code Online (Sandbox Code Playgroud)
但比以下几行:
#3)
var dbMigrator = new DbMigrator(_configuration);
dbMigrator.Update();
Run Code Online (Sandbox Code Playgroud)
抛出错误:
数据库中已经有一个名为"UserProfiles"的对象.
嗯,这很有意义,因为迁移尝试创建刚刚由WebSecurity创建的表.
我找到了一个解决方法:我把#2放在了#1之上.比它工作.
问题是我必须在种子方法中初始化WebSecurity,这有点臭.
我的问题是如何将WebSecurity.InitializeDatabaseConnection移回Global.asax?
我创建了一个新的MVC 5项目,当我在Package Manager控制台中获得以下PS异常时,我尝试为我创建的Code First实体启用迁移:
One or more validation errors were detected during model generation:
Project.Web.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
Project.Web.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
Run Code Online (Sandbox Code Playgroud)
问题是我的项目中没有调用IdentityUserRole或没有类IdentityUserLogin.这些名称位于Microsoft.AspNet.Identity.EntityFramework命名空间中的类,但错误是说这些类存在于我的项目命名空间中.
我该如何解决这个问题?我已经多次清理并重新编译了该项目,似乎没有什么能解决这个问题.
如果我删除我的 …
c# entity-framework ef-code-first ef-migrations entity-framework-6
我正在尝试创建一个有点简单的InventoryTracker并且难以播种我的测试数据库.我目前正在尝试重置数据库并使用以下命令执行全新的代码优先迁移:
- update-database -targetmigration:"0" -force -verbose
- *Delete Current Migrations
- add-migration InitialCreate
- update-database
Run Code Online (Sandbox Code Playgroud)
当我运行update-database -targetmigration:"0" -force -verbose了Package Manager Console回报:The property 'Model_Id' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection<T> where T is a valid entity type.
这是我用我Model的名字INV_Models.cs Model或我忽略的东西的一个简单问题吗?目前,该应用程序已成功构建,但新迁移失败.
我的主要模式是INV_Assets具有[ForeignKey]值Manufacturers, …
我有一个数据库,不小心删除了__MigrationHistory表.现在该程序抛出错误,我不能丢失所有数据.反正有没有恢复我删除的表?我会丢失所有数据库吗?
我手动添加了__MigrationHistory,现在这是错误:
附加信息:无法更新数据库以匹配当前模型,因为存在挂起的更改并且已禁用自动迁移.将挂起的模型更改写入基于代码的迁移或启用自动迁移.将DbMigrationsConfiguration.AutomaticMigrationsEnabled设置为true以启用自动迁移
我正在尝试对现有数据库使用迁移,并创建一个新数据库(如果不存在)。
EF Core文档说:
如果在数据库已经存在时创建了初始迁移,则会生成数据库创建代码,但由于数据库已经与数据模型匹配,因此不必运行该代码。当您将应用程序部署到尚不存在数据库的另一个环境时,此代码将运行以创建您的数据库
我进行了初始迁移,它创建了一个上下方法来创建我的表。当我将其运行到新数据库(连接字符串中的新数据库名称)时,它将按预期创建数据库和表。
但是,如果我将其运行到现有数据库(不是由迁移创建的)上,则它在尝试创建已经存在的表的第一种启动方法上将失败,并且迁移将停止运行。
Docs说“不必运行”,但是迁移要做的第一件事就是尝试创建一个现有表。
如何防止迁移尝试创建现有表?迁移中是否内置了类似“如果不存在则创建”的内容?文档正确吗?它应该按预期/在文档中描述吗?
如果我做错了,与现有数据库和新数据库上的迁移一起使用的迁移策略是什么?
Dotnet Core版本:1.1。EFCore版本:1.1.2。
提前致谢。
我在我的项目中使用实体框架代码第一种方法.我在包管理器控制台中使用enable-migrations语句创建了模型并启用了迁移.我的上下文类包含映射配置和关系
modelBuilder.Configurations.Add(new PostMap());
modelBuilder.Configurations.Add(new UserMap());
Run Code Online (Sandbox Code Playgroud)
我的迁移文件中的Up方法201302261054023_InitialCreate.cs包含所有表定义,作为通过DbSet <Type>属性指定的上下文.
我在自定义数据库初始化程序中更改了InitializeDatabase方法,因为我想在数据库初始化期间迁移到特定的迁移.
public void InitializeDatabase(T context)
{
bool databaseExists = context.Database.Exists();
if (!databaseExists)
context.Database.CreateIfNotExists();
var configuration = new Configuration();
var migrator = new DbMigrator(configuration);
migrator.Update("InitialCreate");
}
Run Code Online (Sandbox Code Playgroud)
可以修改201302261054023_InitialCreate类中的Up方法来添加另一个索引
CreateTable(
"dbo.City",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(maxLength: 450),
})
.PrimaryKey(t => t.Id);
Run Code Online (Sandbox Code Playgroud)
至
CreateTable(
"dbo.City",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(maxLength: 450),
})
.PrimaryKey(t => t.Id)
.Index(t=>t.Name,true);
Run Code Online (Sandbox Code Playgroud)
没有附加另一个迁移?
entity-framework ef-code-first ef-migrations entity-framework-5
我想将[Required]属性添加到包含NULL值的现有数据库记录中,以及使用Code First Migrations自动生成数据库本身的位置.模型架构更新可能如下所示:
public class Product // BEFORE UPDATE
{
[Key]
public int ID { get; set; }
[Required]
public string Name { get; set; }
public string Color { get; set; }
}
public class Product // AFTER UPDATE
{
[Key]
public int ID { get; set; }
[Required]
public string Name { get; set; }
[Required] // <---- Added
public string Color { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我运行命令
add-migration MakeColorRequired // Runs successfully
update-database // Crashes …Run Code Online (Sandbox Code Playgroud) Update-Database命令,但此命令不存在!entity-framework ef-migrations entity-framework-core .net-core aspnetboilerplate
我正在使用Entity Framework代码优先方法.我收到以下错误,当我运行Update-Database -script在一个空数据库,运行程序后enable-migrations和add-migration:
standardvba.DataAccessLayer.LessonQuestion :: EntityType'LessonQuestion'没有定义键.定义此EntityType的键.
LessonQuestions:EntityType:EntitySet'LessonQuestions'基于没有定义键的类型'LessonQuestion'.
LessonQuestion.cs:
public partial class LessonQuestion
{
public LessonQuestion()
{
this.LessonQuestionDetails = new List<LessonQuestionDetail>();
}
[Key]
public int QuestionID; // Key is defined
public int OrderNumber;
[ForeignKey("ParentQuestion")]
public int ParentQuestionID;
[ForeignKey("Lesson")]
public int LessonID { get; set; }
[ForeignKey("ActionedBy")]
public int ActionedByUserID { get; set; }
[MaxLength(1000)]
public string Question { get; set; }
public bool IsApproved { get; set; }
[Required] // Sets the CASCADE constraint, …Run Code Online (Sandbox Code Playgroud) 它是这样的:
public class ApplicationUser : IdentityUser
{
// omitted for the sake of brevity...
public string LastName { get; set; }
public string FirstName { get; set; }
public string Email { get; set; }
}
public class Invoice
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Index(IsUnique = true)]
[Required]
public int No { get; set; }
[Required]
public DateTime Created { get; set; }
[Required]
public string ManagerId { get; set; }
public virtual ApplicationUser Manager …Run Code Online (Sandbox Code Playgroud) ef-migrations ×11
c# ×6
.net-core ×2
.net ×1
asp.net-mvc ×1
foreign-keys ×1
key ×1
sql ×1
sql-server ×1