Tag: entity framework

在Entity Framework 4中默认禁用延迟加载

EF4默认启用延迟加载。 至less,在我的项目中,我可以看到价值 dataContext.ContextOptions.LazyLoadingEnabled 默认为true。 我不想懒加载,我不想写: dataContext.ContextOptions.LazyLoadingEnabled = false; 每次我得到一个新的上下文。 那么有没有办法在整个项目中默认closures它?

DataContractSerializer错误使用entity framework4.0与WCF 4.0

我试图通过WCF从entity framework检索对象的列表,但我收到以下exception: 尝试序列化参数http://tempuri.org/:GetAllResult时发生错误。 该消息的InnerException是“types‘System.Data.Entity.DynamicProxies.TestObject_240F2B681A782799F3A0C3AFBE4A67A7E86083C3CC4A3939573C5410B408ECCE’数据合同名称‘TestObject_240F2B681A782799F3A0C3AFBE4A67A7E86083C3CC4A3939573C5410B408ECCE: http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies预计不会’。 考虑使用DataContractResolver或将任何不知道的types静态添加到已知types列表中 – 例如,通过使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知types列表中。 请参阅InnerException获取更多细节。 我曾经使用WCF,但从来没有与entity framework。 我已经通过entity framework生成了所有实体,并使用[DataContract]和[DataMember]属性进行注释。 我的任何实体上都没有导航属性。 被调用的GetAll()方法在抽象服务类中: [ServiceContract] public interface IService<T> { [OperationContract] List<T> GetAll(); } 我正在使用ChannelFactory来调用我的实现: Binding binding = new NetTcpBinding(); EndpointAddress endpointAddress = new EndpointAddress("net.tcp://localhost:8081/" + typeof(TestObjectService).Name); using (ChannelFactory<ITestObjectService> channel = new ChannelFactory<ITestObjectService>(binding, endpointAddress)) { ITestObjectService testObjectService = channel.CreateChannel(); testObjects = testObjectService.GetAll(); channel.Close(); } 我主持这样的: […]

如何在EF中更新父实体时添加/更新子实体

这两个实体是一对多的关系(由第一代stream利的API构build)。 public class Parent { public Parent() { this.Children = new List<Child>(); } public int Id { get; set; } public virtual ICollection<Child> Children { get; set; } } public class Child { public int Id { get; set; } public int ParentId { get; set; } public string Data { get; set; } } […]

Linq:有条件地给where子句添加条件

我有这样的查询 (from u in DataContext.Users where u.Division == strUserDiv && u.Age > 18 && u.Height > strHeightinFeet select new DTO_UserMaster { Prop1 = u.Name, }).ToList(); 我想根据这些条件是否提供给运行此查询的方法来添加各种条件,如年龄,身高。 所有条件将包括用户司。 如果年龄提供,我想添加到查询。 相似,如果提供了高度,我想补充一点。 如果这是使用sql查询完成的,我会使用string生成器将它们追加到主strSQL查询中。 但是在Linq中,我只能想到使用一个IF条件,在这个条件下,我将三次写入相同的查询,每个IF块都有一个额外的条件。 有一个更好的方法吗? 谢谢你的时间..

我怎样才能从我的程序中的DbContext.SaveChanges()生成SQL?

根据这个线程,我们可以通过EFlogging生成的SQL ,但是DbContext.SaveChanges()呢? 有没有简单的方法来做这个工作,没有任何额外的框架?

entity framework代码第一个date字段创build

我正在使用entity framework代码优先方法来创build我的数据库表。 下面的代码在数据库中创build一个DATETIME列,但是我想创build一个DATE列。 [DataType(DataType.Date)] [DisplayFormatAttribute(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")] public DateTime ReportDate { get; set; } 如何在创build表格时创buildDATEtypes的列?

在单独assembly中启用具有上下文的迁移?

我有一个项目,我想运行我的update-database ,但我有我的模型和上下文在一个单独的项目。 如果我运行enable-migrations我得到这个错误:在程序集“MyProject”中找不到上下文types。 这大概是因为我的上下文是在MyProject.MVC。 如果我对MyProject.MVC运行enable-migrations ,则必须添加一个应用程序configuration文件。 我不想这样做,因为我想在许多项目中使用代码。 所以我可以运行对MyProject的enable-migrations ,并以某种方式告诉它在MyProject.MVC的上下文?

entity framework迁移重命名表和列

我重命名了一对夫妇实体及其导航属性,并在EF 5中生成了新的迁移。与EF迁移中的重命名一样,默认情况下,它将删除对象并重新创build它们。 这不是我想要的,所以我几乎不得不从头构build迁移文件。 public override void Up() { DropForeignKey("dbo.ReportSectionGroups", "Report_Id", "dbo.Reports"); DropForeignKey("dbo.ReportSections", "Group_Id", "dbo.ReportSectionGroups"); DropForeignKey("dbo.Editables", "Section_Id", "dbo.ReportSections"); DropIndex("dbo.ReportSectionGroups", new[] { "Report_Id" }); DropIndex("dbo.ReportSections", new[] { "Group_Id" }); DropIndex("dbo.Editables", new[] { "Section_Id" }); RenameTable("dbo.ReportSections", "dbo.ReportPages"); RenameTable("dbo.ReportSectionGroups", "dbo.ReportSections"); RenameColumn("dbo.ReportPages", "Group_Id", "Section_Id"); AddForeignKey("dbo.ReportSections", "Report_Id", "dbo.Reports", "Id"); AddForeignKey("dbo.ReportPages", "Section_Id", "dbo.ReportSections", "Id"); AddForeignKey("dbo.Editables", "Page_Id", "dbo.ReportPages", "Id"); CreateIndex("dbo.ReportSections", "Report_Id"); CreateIndex("dbo.ReportPages", "Section_Id"); […]

如何用entity framework通过id删除一个对象

在我看来,我必须检索一个对象,然后用下面的entity framework删除它 var customer = context.Customers.First(c => c.Id = 1); context.DeleteObject(customer); context.Savechanges(); 所以我需要打两次数据库。 有更简单的方法吗?

entity framework – 生成类

我有一个现有的数据库。 我希望有一种方法来从这个数据库生成类文件。 但是,我似乎看到很多从类文件生成数据库。 有没有办法使用entity framework从现有的数据库生成类文件? 如果这样怎么样? 有人可以指点我的教程吗?