Tag: entity framework

为什么EF返回代理类而不是实际实体?

当我想要实际的实体类时,我遇到了entity framework返回代理的问题。 我第一次运行我的代码时,一切都正常运行(没有代理),但每次迭代之后,我的一个DbSets总是返回代理而不是实际类型。 我在每次迭代后处理上下文,所以我不明白为什么第一次通过它工作,并且每次都没有。 我的代码在这一行失败了。 我的所有POCO都设置了Table属性,但因为它返回了一个代理类,所以没有table属性。 TableAttribute attrib = (TableAttribute)attributes.Single(); 在我销毁对象之后,DbContext中是否存在一些静态魔法? 我使用以下内容将对象移动到内存中 MajorClasses = ctx.MajorClasses.ToArray(); 我也试过了 MajorClasses = ctx.MajorClasses.AsNoTracking().ToArray(); 在我的OnModelCreating中,我有以下设置 base.Configuration.ProxyCreationEnabled = false; base.Configuration.LazyLoadingEnabled = false;

entity framework,自动应用迁移

我首先使用Entity Framework代码,并通过以下代码设置AutomaticMigrationsEnabled为true: Database.SetInitializer(new MigrateDatabaseToLatestVersion()); ////////////////////////////////// public class MigrateDBConfiguration : System.Data.Entity.Migrations.DbMigrationsConfiguration { public MigrateDBConfiguration() { AutomaticMigrationsEnabled = true; AutomaticMigrationDataLossAllowed = true; } } 在第一次运行项目时,它可以正常工作并创建数据库和表。 在我更改模型并删除一些字段或添加新字段并运行Add-Migration之后,生成了迁移类但在运行项目之后发生此exception: EntityFramework.dll中出现“System.InvalidOperationException”类型的exception,但未在用户代码中处理 附加信息:自创建数据库以来,支持“DBContext”上下文的模型已更改。 编辑:像arturo menchaca的回答我改变可能代码如下: protected override void OnModelCreating(DbModelBuilder modelBuilder) { Database.SetInitializer(new MigrateDatabaseToLatestVersion<DBContext, MigrateDBConfiguration>()); … 但例外情况是: 数据库中已经有一个名为“MyTable”的对象。 我想申请迁移。

为什么Entity Framework会生成嵌套的SQL查询?

为什么Entity Framework会生成嵌套的SQL查询? 我有这个代码 var db = new Context(); var result = db.Network.Where(x => x.ServerID == serverId) .OrderBy(x=> x.StartTime) .Take(limit); 哪个产生了这个! (注意双选语句) SELECT `Project1`.`Id`, `Project1`.`ServerID`, `Project1`.`EventId`, `Project1`.`StartTime` FROM (SELECT `Extent1`.`Id`, `Extent1`.`ServerID`, `Extent1`.`EventId`, `Extent1`.`StartTime` FROM `Networkes` AS `Extent1` WHERE `Extent1`.`ServerID` = @p__linq__0) AS `Project1` ORDER BY `Project1`.`StartTime` DESC LIMIT 5 我应该更改什么才能产生一个选择语句? 我正在使用MySQL和Entity Framework与Code First。 更新 无论传递给OrderBy()方法的参数类型如何,我都有相同的结果。 更新2:定时 […]

ASP.net MVC – 我应该使用ViewModel中的AutoMapper到Entity Framework实体吗?

我目前正在使用AutoMapper将我的Entity Framework实体映射到我的View Model: public class ProductsController : Controller { private IProductRepository productRepository; public ProductsController(IProductRepository productRepository) { this.productRepository = productRepository; } public ActionResult Details(int id) { var product = productRepository.GetProduct(id); if( product == null ) return View(“NotFound”); ProductDetailsViewModel model = Mapper.Map(product); return View(model); } } 这很好用。 我的问题是当我需要从我的View Model转到我的实体以更新数据库时。 我应该使用AutoMapper吗? 这是一个糟糕/危险的做法吗? 看起来AutoMapper很适合将复杂类型展平为简单(平面)类型,但到目前为止,我正在努力尝试从平面/简单到更复杂的类型,例如具有各种导航属性的实体。 如果使用AutoMapper执行此操作是个坏主意,那么我的代码对于Create操作会是什么样子? public ActionResult Create(CreateProductViewModel model) […]

如何在绑定到链接到EF4实体的绑定源时对DataGridView进行排序

我有一个链接到BindingSource的DataGridView 。 我的BindingSource链接到一个IQueryable实体列表: public void BindTo(IQueryable elements) { BindingSource source = new BindingSource(); source.DataSource = elements; bindingNavigator1.BindingSource = source; dataGridView1.DataSource = source; } 我希望我的用户能够点击网格标题来对数据进行排序 – 努力使其发挥作用。 可能吗? 如果是这样,我该怎么办?

适用于Asp.Net MVC 5应用程序中所有模型的Models.ApplicationDbContext?

我正在创建一个Asp.Net MVC 5网站。 我需要在ApplicationUser中添加自定义字段并将其与其他模型关联(添加外键) 。 我想我应该只使用一种上下文类型。 但是,代码脚手架已生成以下ApplicationDbContext类。 我可以把我所有的public DbSet … { get; set; } public DbSet … { get; set; } 在课堂上? 还是有更好的模式? namespace MyApp.Models { // You can add profile data for the user by adding more properties to your User class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more. public class ApplicationUser : User { […]

在Visual Studio 2013中使用EF6的MySQL连接器

我试图通过C#.NET web MVC应用程序连接到MySQL。 我的问题是,当我尝试根据我的MySQL连接添加从数据库生成的ADO.NET实体数据模型时,我收到以下错误消息: 您的项目引用了最新版本的Entity Framework; 但是,无法为您的数据连接找到与此版本兼容的Entity Framework数据库提供程序。 在执行此操作之前,请退出此向导,安装兼容的提供程序并重建项目 我正在运行以下软件,升级和附件: Visual Studio 2013 MySQL Server v5.6.21 MySQL For Visual Studio v1.2.3 Connector / NET v6.9.4 NuGet包: EntityFramework v6.1.1 MySQL.Data v6.9.3 MySQL.Data.Entities v6.8.3.0 SQL.Web v6.9.3 我的web.config,entityFramework块: .. .. 我已经查看了我可以在这里找到的所有其他解决方案,通常通过谷歌,尝试了所有这些解决方案似乎没有工作或已经用于非常不同版本的Connector / NET或其他所需的程序。 谁能发现我做错了什么?

ASP.NET MVC 4,EF5,模型中的独特属性 – 最佳实践?

ASP.NET MVC 4,EF5, Code First ,SQL Server 2012 Express 在模型中强制使用唯一值的最佳做法是什么? 我有一个具有’url’属性的places类,对于每个地方都应该是唯一的。 public class Place { [ScaffoldColumn(false)] public virtual int PlaceID { get; set; } [DisplayName(“Date Added”)] public virtual DateTime DateAdded { get; set; } [Required(ErrorMessage = “Place Name is required”)] [StringLength(100)] public virtual string Name { get; set; } public virtual string URL { get; […]

如何使用Entity Framework ASP.Net MVC 5删除多个记录?

我有如下图所示的表格: 如何使用基于ProjectId的Entity FrameWork删除表的所有记录?

EF Core中的多个Includes()

我有一个扩展方法,允许您在EF中一般包含数据: public static IQueryable IncludeMultiple(this IQueryable query, params Expression<Func>[] includes) where T : class { if (includes != null) { query = includes.Aggregate(query, (current, include) => current.Include(include)); } return query; } 这允许我在我的存储库中有这样的方法: public Patient GetById(int id, params Expression<Func>[] includes) { return context.Patients .IncludeMultiple(includes) .FirstOrDefault(x => x.PatientId == id); } 我相信扩展方法在EF Core之前有效,但现在包括“children”就像这样: var blogs = context.Blogs […]