Tag: entity framework

仅更新未设置为null的EF实体上的某些属性

我有一个浏览器发送JSON但它只包含已更改的给定模型的属性。 因此,一旦WCF DataContractJsonSerializer完成它的工作,我就有一个对象,可能只填充了ID和Description字段。 将其附加到DbContext将导致描述字段被更新,但所有其他字段被设置为其类型在数据库中的默认值。 这是因为如果WCF没有看到JSON中指定的属性,那么它将跳过它,这意味着实例中的属性将根据自动实现的属性使用类型默认值。 所以这意味着我需要决定在没有访问JSON本身的情况下传递了哪些字段。 事实上,它可能是线上的XML,所以我可以使用的是这个部分序列化的对象。 最合乎逻辑的是使用null作为特殊值,这意味着此属性尚未被序列化。 所以在POCO模型的构造函数中,我将所有属性设置为null 。 在Update方法中,然后使用此序列化对象作为存根。 我必须遍历每个属性,如果值没有设置为null,那么我将其状态设置为modified。 据我所知,这是没有任何副作用的工作,但我只是不确定这是做这样的事情的方式。 它添加的一个限制是客户端不能再故意将属性设置为null,因为该更新将丢失。 解决此问题的一种方法是使用一个特殊的int值,可以设置为在数据库中表示null,也可以是一个空字符串,表示数据库中的null,并在更新中使用代码查找这些特殊值,然后设置实体属性为null。 远非理想,可能容易出错。 这是我目前处理更新的代码。 我真的非常感谢有关更好,也许更明显的做法的建议。 夏季:如何判断模型实例上的哪些属性已由DataContractSerializer / DataContractJsonSerializer设置,哪些属性仅使用其构造函数的默认值。 使用特殊值是有问题的,因为客户端可能希望将某些内容设置为空字符串,或者设置为0或-1或实际上为null。 public T Update(T obj) { var entity = ds.Attach(obj); // For each property in the model foreach (var p in typeof(T).GetProperties()) { // Get the value of the property var v = p.GetValue(obj, […]

如果在Code First模式下使用,使用T4模板为Database First和Model First开发生成的代码可能无法正常工作

我在我的asp.net MVC 4应用程序中使用entity framework工作。 我有一个edmx文件。 我试图使用这个EF模型中的实体来填充像这样的视图模型: using (var context = new VehiclesContext()) { IEnumerable vehicles = context.Vehicles.Select(x => new SearchedVehicles { Year = x.Year, Make = x.Make, Model = x.Model, Mileage = x.Mileage, VIN = x.VIN }); return View(vehicles); } Vehicle是edmx中的实体,其中SearchedVehicles是viewmodel但是我得到了这个exception: Code generated using the T4 templates for Database First and Model First development may […]

ObjectContext.GetObjectType(e.GetType())不返回POCO实体的实体类型

ObjectContext.GetObjectType方法应返回“与指定类型的代理对象关联的POCO实体的实体类型” 那么为什么我的代码只返回代理? 我正在使用entity framework6发布候选版 //Soft delete var e = Context.Set().Find(id); e.IsDeleted = true; InsertOrUpdate(e); Type t = System.Data.Objects.ObjectContext.GetObjectType(e.GetType()); string name = t.Name; //Property_6C887DE7274181E6E99D6FCF2C21BDD59E226F99B0064F59954E70062C135331 //Surely I shouldn’t have to use Substring here? name = name.Substring(0, name.IndexOf(“_”)).ToSpacedTitleCase(); string message = name + ” deleted”;

如何使用数据库第一种方法命名外键

有没有办法改变entity framework的命名约定? 示例: 我有2张桌子 Planification ————– creator_id guest_id Profile ————– profile_id creator_id和guest_id都是profile_id外键 默认情况下,实体会生成这样的规划化类 public string guest_id { get; set; } public string creator_id { get; set; } public virtual Profile Profile { get; set; } public virtual Profile Profile1 { get; set; } 我更喜欢像Profile和Creator这样比Profile和Profile1更具体的东西。 有没有办法改变命名约定,因为我感到内疚让它像这样。

无法将类型System.Collections.Generic.IEnumerable 隐式转换为bool

我正在开发一个ASP.NET MVC 4应用程序,我正在尝试在Entity Framework 5中运行这个Lambda表达式。 var customer = db.GNL_Customer.Where(d => d.GNL_City.FKProvinceID == advancedProvinceID || advancedProvinceID == null) .Where(d => d.FKCityID == advancedCityID || advancedCityID == null) .Where(d => d.FKDepartmentStoreID == advancedDepartmentStoreID || advancedDepartmentStoreID == null) .Where(d => d.GNL_CustomerLaptopProduct.Where(r => String.Compare(r.BrandName, brandID) == 0 || brandID == null)); 我收到此错误: Cannot implicitly convert type ‘System.Collections.Generic.IEnumerable’ to ‘bool’ […]

错误:项目上未安装EntityFramework包

我刚刚安装了SQL Server 2008,将我的ASP.NET MVC 4项目配置为部署在本地IIS,并添加了一个新的连接到数据库(在sql server 2008中)。 当我尝试通过Package Manager控制台中的 Enable-Migrations -ContextTypeName MonitoringNDataContext启用迁移(entity framework)时。 我得到以下错误: 项目’MonitoringN.Tests’上未安装EntityFramework包。 请问有什么好主意吗? MonitoringN:命名ASP.NET MVC 4项目。 MonitoringNDataContext:放入MonitoringN的我的DBContext类的名称。 MonitoringN.Tests:与MonitoringN相关的unit testing项目的名称。

EF 6懒惰加载禁用,但儿童记录负载无论如何

我先使用EF6代码。 有两个表, Lesson和LessonSections 。 LessonSections表具有LessonSections的外键 这是Lesson类,没有删除任何重要字段: public partial class Lesson { [System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”, “CA2214:DoNotCallOverridableMethodsInConstructors”)] public Lesson() { LessonSections = new HashSet(); } [StringLength(50)] public string Id { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”, “CA2227:CollectionPropertiesShouldBeReadOnly”)] public virtual ICollection LessonSections { get; set; } } 以下是我启动数据模型的方法: var db = new Touch_Type_Trainer_DB.DataModel(); db.Configuration.ProxyCreationEnabled = false; db.Configuration.LazyLoadingEnabled = false; 在我第一次调用数据库以检索数据库中的第一课后,生成的对象没有LessonSections 然后我再次调用将这些部分检索到一个单独的对象中。 (它们必须位于单独的对象中,因为我想将它们序列化为JSON字符串,并且如果我使用标准EF […]

在发现’EntityFrameworkConfiguration’类型之前使用了默认的DbConfiguration实例

public class EntityFrameworkConfiguration : DbConfiguration { public EntityFrameworkConfiguration() { this.SetModelCacheKey(ctx => new EntityModelCacheKey((ctx.GetType().FullName + ctx.Database.Connection.ConnectionString).GetHashCode())); } } 为了使上面的代码工作,我在web.config中添加了以下行 但对于我正在使用程序集引用的其他项目,我得到例外: {“在发现’EntityFrameworkConfiguration’类型之前,entity framework使用了默认的DbConfiguration实例。必须在使用任何entity frameworkfunction之前在应用程序启动时设置’EntityFrameworkConfiguration’实例,或者必须在应用程序的配置文件中注册。 http://go.microsoft.com/fwlink/?LinkId=260883了解更多信息。“}

绑定时已释放ObjectContext实例

这是我的代码: public class JobsRepository:BaseNewsletterRepository { public IEnumerable GetJobs() { var jobs = Newsletterctx.Jobs.Include(“Info”).OrderBy(o => o.JobID); return jobs.AsEnumerable(); } } public class BusinessNewsletter { public static IEnumerable GetJobs() { using (JobsRepository jobsRepository = new JobsRepository()) { return jobsRepository.GetJobs(); } } } rptJobs.DataSource = BusinessNewsletter.GetJobs(); rptJobs.DataBind(); 当我尝试绑定时,我得到错误: ObjectContext实例已被释放,不能再用于需要连接的操作。 是唯一的解决方案,在BusinessNewsletter类中转换和返回List吗? public static IList GetJobs() { IList job; using […]

从单独的列添加Entity Framework中的日期时间和时间

我想得到所有尚未结束的事件 select * FROM [Events] where EndDate+cast(EndTo as datetime)>GETDATE() 这是完美的。 这是我的价值( EndTo: 11:00:00.0000000, END DATE: 2016-05-26 ) 我试过用 var list = dbContext.Events .Where(e => e.EndDate + e.EndTo > DateTime.Now) .ToList(); 而且我收到了一个错误 DbArithmeticExpression参数必须具有数字公共类型。 如何在Entity Framework中编写此查询?