我迷迷糊糊uppon同样的问题在描述这个问题。此外,我不想从数据库中丢失 __migrationHistory 表。
我使用包含所有 DbSet<> 的“超级”上下文并使用普通上下文的建议解决方案进行了尝试,但出现错误。(“模型支持 DbContext 已更改”)如果您只是从 SQL 服务器中删除 __migrationHistory 表,这很容易避免,但正如我所说,我想保留历史记录。
我找到了一个简单易行的解决方案,请参阅下面的答案。
c# entity-framework ef-code-first entity-framework-migrations
我正在考虑将 Entity Framework 6 用于一个新项目。
当我们开发我们的表时,我们添加一个 int 类型的更新计数器,并且在执行更新时我们检查它没有改变并在成功更新时增加它。
从我读过的内容来看,我可以通过向更新计数器属性添加 ConcurrencyCheck 属性来进行检查,但是如何在成功更新时执行增量?
或者我应该只使用 Rowversion 列?
我创建了一个代码 - 第一个实体框架迁移并应用于数据库,有没有办法可以重命名这个迁移?
entity-framework ef-code-first ef-migrations entity-framework-6
我已经设置了我的EF代码优先数据库,但想要添加其他派生属性.(是的,它应该在视图模型中,我们可以讨论另一种时间为什么这样.)我创建了一个扩展实际表类的部分类.如果我添加一个[NotMapped]新的部分,它会避免映射我在那里添加的其他属性还是它将应用于整个类?
我们的项目使用Entity Framework 6.0和.NET 4.5,FAT-Client和Code-First方法.
我们有大约20个迁移文件(C#部分类)通过Add-MigrationVisual Studio包管理器控制台中的cmdlet自动生成,并通过成功应用Update-Database.
现在,我们的客户端有一个集成数据库,已经应用了大约10个迁移,我们现在需要应用剩余的10个迁移.我们使用Update-Database -Script -SourceMigration:<migration10>它来为剩余的迁移生成SQL脚本.在这种情况下 - 以及使用时SourceMigration:$InitialDatabase- 会显示以下错误:
[...]
Applying explicit migration: 201609141617112_HostAufbauIdentifier.
Applying explicit migration: 201609141622583_RemPerStaWe.
System.Xml.XmlException: 'SoftwareAuftrag_Auftrag' is an unexpected token. Expecting white space. Line 1943, position 92.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
at System.Xml.XmlTextReaderImpl.ThrowExpectingWhitespace(Int32 pos)
at System.Xml.XmlTextReaderImpl.ParseAttributes()
at System.Xml.XmlTextReaderImpl.ParseElement()
at System.Xml.XmlTextReaderImpl.ParseElementContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r)
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o)
at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
at System.Xml.Linq.XDocument.Load(Stream stream, LoadOptions options)
at …Run Code Online (Sandbox Code Playgroud) 我的实体有一个允许为null的属性.但是,如果它不为null,那么它必须是唯一的.换句话说,该列是唯一的但允许多个空值.
我试过了:
config.Property(p => p.ProductId).IsRequired(false);
Run Code Online (Sandbox Code Playgroud)
我记得在前核心EF中努力工作.
这可能吗?如何配置实体?
我有2个模型,其中一个模型具有另一个模型的子集合:
[Table("ParentTable")]
public class Parent
{
[Key, Column("Parent")]
public string Id { get; set; }
[Column("ParentName")]
public string Name { get; set; }
public virtual ICollection<Widget> Widgets { get; set; }
}
[Table("WidgetTable")]
public class Widget
{
public string Year { get; set; }
[Column("Parent")]
public string ParentId { get; set; }
public string Comments { get; set; }
[Key, Column("ID_Widget")]
public int Id { get; set; }
[ForeignKey("ParentId"), JsonIgnore]
public virtual Parent Parent { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
此代码适用于99%以上的小部件: …
我有以下代码,我正在尝试使用ef core更新ClientAccount,但是它们并发检查失败:
public class ClientAccount
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }
[Required]
[ConcurrencyCheck]
public double Balance { get; set; }
[Required]
public DateTime DateTimeCreated { get; set; }
[Required]
public DateTime DateTimeUpdated { get; set; }
}
public class ClientRepository
{
private readonly MyContext context;
public ClientRepository(MyContext context)
{
this.context = context;
}
public ClientAccount GetClientAccount()
{
return (from client in context.ClientAccount
select client).SingleOrDefault();
}
public void Update(ClientAccount client)
{
context.Update(client);
context.Entry(client).Property(x => x.DateTimeCreated).IsModified = false; …Run Code Online (Sandbox Code Playgroud) 我正在使用ASP.net Core 2和EntityFrameWorkCore 2.1.0构建一个新项目,我开始构建数据库结构.现在我必须进行第一次迁移以使我的数据库准备就绪,当我在Package Manager控制台中执行'Add-Migration Init'时,我收到此错误:
The current CSharpHelper cannot scaffold literals of type 'Microsoft.EntityFrameworkCore.Metadata.Internal.DirectConstructorBinding'. Configure your services to use one that can.
Run Code Online (Sandbox Code Playgroud)
我也尝试了这个文档https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations,我收到的消息是:
Unable to create an object of type 'ApplicationDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<ApplicationDbContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.
Run Code Online (Sandbox Code Playgroud)
我也跟着这篇文章没有任何成功.
如果我尝试没有Microsoft.AspNetCore.Identity.EntityFrameworkCore 2.0.2并使用简单的2表结构,我能够使它工作.
所以现在我需要你的帮助......
我正在使用Entity Framework(采用代码优先方法),并且使用预期的外键和唯一键约束成功创建了数据库。
我有这两个模型类:
public class Foo
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public Bar Bar { get; set; }
}
public class Bar
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
[Index(IsUnique = true), StringLength(512)]
public string Url { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
而这个应用程序代码:
var foo = GetData();
using (DatabaseContext db = new DatabaseContext())
{
db.Entry(foo).State = EntityState.Added;
if (foo.Bar != null)
{
var bar = await db.Bar.FirstOrDefaultAsync(x => x.Url == foo.Bar.Url);
if (bar …Run Code Online (Sandbox Code Playgroud) c# entity-framework navigation-properties data-annotations ef-code-first
ef-code-first ×10
c# ×9
.net ×1
asp.net-core ×1
code-first ×1
ef-core-2.0 ×1