我想通过将DbCompiledModel缓存到磁盘来减少EF6中的启动时间.
为DbContext编写EDMX文件很容易:
EdmxWriter.WriteEdmx(myDbContext, XmlWriter.Create(@"C:\temp\blah.xml"))
Run Code Online (Sandbox Code Playgroud)
并且很容易将DbCompiledModel传递给DbContext:
var db = new DbContext(connectionString, myDbCompiledModel)
Run Code Online (Sandbox Code Playgroud)
但是,似乎没有任何方法可以将EDMX文件从磁盘读入DbCompiledModel!我怎样才能做到这一点?
请注意,我已在此分支版本的EF6中使用EdmxReader工具成功实施了该解决方案:
https://github.com/davidroth/entityframework/tree/DbModelStore
Run Code Online (Sandbox Code Playgroud)
但是,我不愿意在生产环境中使用分支版本.我试过从这个分支中提取EdmxReader实用程序,但它依赖于我无法访问的DbCompiledModel的内部构造函数.
那么,我如何从磁盘获取EDMX文件并将其转换为DbCompiledModel?
今天是悲伤的一天.今天第一件事我看到EF异常说" 自创建数据库以来,支持'DomainContext'上下文的模型已经改变了. " 接近午夜,我仍然看到这个错误.这是我职业生涯的终结 - (
我很确定模型中没有任何变化,但错误出现了.我尝试过创建一个新的迁移,它是空的:
public void Up()
{
}
public void Down()
{
}
Run Code Online (Sandbox Code Playgroud)
应用此迁移没有任何好处 - 错误仍然存在.我使用了常见的建议将初始化设置为null:
Database.SetInitializer<DomainContext>(null);
Run Code Online (Sandbox Code Playgroud)
当我访问数据库时,它使错误消失了.但这让我非常困扰 - 每当我尝试通过代码运行迁移时,我再次看到类似的错误:
var configuration = new Migrations.Configuration();
configuration.TargetDatabase = new DbConnectionInfo("correct connection string", "System.Data.SqlClient");
var migrator = new DbMigrator(configuration);
migrator.Update(); // <<-- exception is thrown here
Run Code Online (Sandbox Code Playgroud)
Exception throw如下所示:System.Data.Entity.Migrations.Infrastructure.AutomaticMigrationsDisabledException:无法更新数据库以匹配当前模型,因为存在挂起的更改并且已禁用自动迁移.将挂起的模型更改写入基于代码的迁移或启用自动迁移.将DbMigrationsConfiguration.AutomaticMigrationsEnabled设置为true以启用自动迁移.
我已更新到EF 6.1(之前是6.0.2),但这没有任何区别.
困扰我的另一件事是我可以通过Nuget控制台运行迁移:
Update-Database
Run Code Online (Sandbox Code Playgroud)
运行良好,不会出现任何问题.但是,当我将DB初始化程序设置为自动运行迁移时:
var initializer = new MigrateDatabaseToLatestVersion<DomainContext, Migrations.Configuration>();
Database.SetInitializer(initializer);
var domainContext = new DomainContext();
domainContext.Database.Initialize(true); // <<-- this throws exception
Run Code Online (Sandbox Code Playgroud)
无法更新数据库以匹配当前模型,因为存在挂起的更改并且已禁用自动迁移.将挂起的模型更改写入基于代码的迁移或启用自动迁移.将DbMigrationsConfiguration.AutomaticMigrationsEnabled设置为true以启用自动迁移.
真正的问题是为什么EF在运行Nuget控制台和迁移DB-Initialiser时对模型有不同的哈希值?我怎样才能找出不同的东西(来自db-state的模型)?以及如何解决这个问题,所以我不必使用hacks(为db-initaliser分配null)?
我正在使用EF 6.1.1和Database First.当我将存储过程导入edmx并生成DBContext时,它看起来像这样:
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<TestSP_Result>("TestSP", params[]...)
Run Code Online (Sandbox Code Playgroud)
返回一个ObjectResult <T>,它实现了IDbAsyncEnumerable <T>所以我这样做是为了读取数据异步:
IDbAsyncEnumerable<T> enumerable = objectResult as IDbAsyncEnumerable<T>;
IDbAsyncEnumerator<T> enumerator = enumerable.GetAsyncEnumerator();
List<T> list = new List<T>();
bool moreItems = await enumerator.MoveNextAsync(CancellationToken.None);
while (moreItems)
{
list.Add(enumerator.Current);
moreItems = await enumerator.MoveNextAsync(CancellationToken.None);
}
return list;
Run Code Online (Sandbox Code Playgroud)
这真的是异步读取数据吗?我附加了探查器,实际的SQL语句在ExecuteFunction行中运行,而不是在枚举结果时.
有没有一种正确的方法从DBContext运行存储过程并异步读取结果?
c# asynchronous entity-framework entity-framework-6 entity-framework-6.1
示例:假设我有这三个类.Foo
是一个DbSet适当的实体框架的实体,而我希望我的EF的DbContext是不知道的Bar
,并Baz
因为我检举了Foo的酒吧财产与我组成SerializedColumn
的属性.通过应用该属性,我希望EF将带有Bazes的Bar实例序列化为单个字符串字段,并在EF实现Foo时透明地将Bar反序列化为Bar对象.
public class Foo
{
public Guid Id { get; set; }
[SerializedColumn]
public Bar Bar { get; set; }
// ..
}
public class Bar
{
public string Name { get; set; }
public Baz[] Baz { get; set; }
// ..
}
public class Baz
{
public string Name { get; set; }
// ..
}
Run Code Online (Sandbox Code Playgroud)
所以Foo的表列看起来像:
[Id] [uniqueidentifier] NOT NULL
[Bar] [nvarchar](max) NULL
Run Code Online (Sandbox Code Playgroud)
当我查询一个时,Foo
我会回来一个Bar
已经反序列化的属性.当我插入或更新Foo …
entity-framework ef-code-first ef-migrations entity-framework-6 entity-framework-6.1
这个问题已经发展,所以我更新了标题.
这是原始标题:Identity 2 UserManager.Find抛出"无效的对象名称'dbo.ApplicationUser'"错误
我正在从SimpleMembership转换为Identity 2.我已经运行了转换脚本并重构了各种文件以供Identity使用.我可以构建并运行应用程序,但是当尝试登录"无效对象名称'dbo.ApplicationUser'"时,会在var user = UserManager.Find(vM.UserName,vM.Password)上抛出错误;
账户管理员:
[RequireHttps]
[Authorize]
public class AccountController : Controller
{
private readonly IUserService _userService;
public UserManager<ApplicationUser> UserManager { get; private set; }
public AccountController()
: this(new UserService(), new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new MyDb()))) { }
public AccountController(IUserService userService, UserManager<ApplicationUser> userManager)
{ _userService = userService; UserManager = userManager; }
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login() { return View(); }
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginVm vM)
{
if (ModelState.IsValid)
{
var …
Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我通过一些迁移启用了Code First Migrations,我还使用SQL Server Compact进行集成测试.
当我运行我的测试时,Entity Framework会创建一个空数据库并尝试在该空数据库上运行迁移并抛出 The specified table does not exist.
基于此报告,我认为实体框架6中迁移的使用已发生变化.
我测试了所有数据库初始化程序,Context.Database.Create();
但在所有情况下都没有创建tabale.
我正在尝试对外键进行简单更新,但脚本永远不会被发送过来.
这是我正在使用的代码:
using (var db = new MyContext())
{
db.Entry<Contact>(newContact).State = EntityState.Modified;
newContact.ContactOwner = db.Person.Find(3);
db.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)
EF6更新Persons表中的其余列,但它不更新Persons表中的Contact_Id.
人物实体:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public List<Contact> ContactList { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
联系实体:
public class Contact
{
public int Id { get; set; }
public string Email { get; set; }
public string TelNo { get; set; }
public Person ContactOwner { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么? …
我了解如何将约定添加到代码优先(通过迁移)项目。我已经成功地执行了表名称,甚至将 GUID Id 字段更改为非聚集。
但我还没有找到如何在未给出名称时更改 EF 提供的默认索引名称。
[Index(IsUnique = true)]
public string Code { get; set; }
[Index]
public string Description { get; set; }
Run Code Online (Sandbox Code Playgroud)
我有这两个要求。顶部索引应命名为UX_[schema]_[table]_Code
,第二个索引应命名为IX_[schema]_[table]_Description
我还需要支持多列索引,其中 IsUnique 仍然是 UX,但列部分是所有列的驼峰式组合(例如, UX_[schema]_[table]_CodeDescription
如果上面的两列位于同一索引中)。
我假设我需要在 IndexAttributeConvention 之后添加它,以便所有当前功能都可以工作并创建 IndexAnnotations。但如果属性(或 Fluent 构造)中留空,我无法找到索引在哪里接收其初始名称。
提前致谢。
entity-framework naming-conventions ef-code-first entity-framework-6.1
这两个陈述之间的区别是什么?
两者都应该删除一个实体.
_context.Entry(new Schoolyear { Id = schoolyearId }).State = EntityState.Deleted;
_context.Schoolyears.Remove(new Schoolyear { Id = schoolyearId });
Run Code Online (Sandbox Code Playgroud)
对于那些不了解EF Extensions的人:
_context.Schoolyears.Delete(s => s.Id == schoolyearId);
Run Code Online (Sandbox Code Playgroud)
多数民众赞成更酷:D
为什么我会得到一个DbUpdateException - 没有进一步的细节 - 当我尝试将新学生插入现有的schoolclassCode时?
这是一对多关系.
var schoolclassCode = await context.SchoolclassCodes.SingleAsync(s => s.Id == pupil.SchoolclassCodeId);
schoolclassCode.Pupils.Add(pupil);
context.Entry(schoolclassCode).State = EntityState.Modified;
int count = await context.SaveChangesAsync();
Run Code Online (Sandbox Code Playgroud)
我是否必须首先在上下文中插入瞳孔.Pupils.add(瞳孔)?
我以为我可以插入瞳孔,并在一个实践中与学校代码相关联
schoolclassCode.Pupils.Add(pupil);
Run Code Online (Sandbox Code Playgroud)
然后将schoolclassCode设置为已修改.
如何在与现有主体/父实体的多对多关系中插入实体?
UPDATE
System.Data.Entity.Infrastructure.DbUpdateException was unhandled by user code
HResult=-2146233087
Message=An error occurred while updating the entries. See the inner exception for details.
Source=mscorlib
StackTrace:
bei System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
bei System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
bei TGB.Repository.PupilRepository.<AddPupil>d__8.MoveNext() in c:\Repository\TGB\TGB.Repository\PupilRepository.cs:Zeile 29.
--- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde ---
bei …
Run Code Online (Sandbox Code Playgroud)