entity-framework-4.1

Dynamic Include statements for eager loading in a query - EF 4.3.1

爷,独闯天下 提交于 2019-11-27 02:15:43
问题 I have this method: public CampaignCreative GetCampaignCreativeById(int id) { using (var db = GetContext()) { return db.CampaignCreatives .Include("Placement") .Include("CreativeType") .Include("Campaign") .Include("Campaign.Handshake") .Include("Campaign.Handshake.Agency") .Include("Campaign.Product") .AsNoTracking() .Where(x => x.Id.Equals(id)).FirstOrDefault(); } } I would like to make the list of Includes dynamic. I tried: public CampaignCreative GetCampaignCreativeById(int id, string[]

EF 4.1 and “Collection was modified; enumeration operation may not execute.” exception

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 02:11:01
问题 This has been driving me nuts for the last 2 days. I have 3 pretty basic classes (well, reduced for readability) public class Employee { public string Name { set; get; } virtual public Employer Employer { set; get; } public Employee(string name) { this.Name = name; } } , // this basically ties Employee and his role in a company. public class EmployeeRole{ public int Id { set; get; } virtual public Employee Employee { set; get; } public string Role { set; get; } public EmployeeRole(Employee

EF Code First Readonly column

醉酒当歌 提交于 2019-11-27 02:07:12
问题 I am using EF Code first with database first approach. "with Database.SetInitializer(null);" My table has two columns createddate and amendddate. They are managed by SQL Server using triggers. The idea is that when data entry happens then these columns gets data via triggers. Now What I want to do is to make this read only from EF Code first point of view. I.e. I want to be able to see the createddate and ameneded dates from my app but I dont want to amend these data. I have tried using

How to map table splitting in EF Code First?

故事扮演 提交于 2019-11-27 02:01:32
How can I map table splitting with EF Code First? Table splitting for EDMX is described for example here . It allows mapping two entities with 1:1 relation into same table. I know I can do the similar mapping with entity and complex type but the big difference is that complex type can't be lazy loaded (or not loaded at all) which is the main reason for table splitting. Here is how I just got EF 4.1 (RC) to do table splitting in Code First. Define your two entities. Make sure to include the key in both entities. Also, include navigation properties in each entity pointing to the other entity. In

EF Code First 4.1 doesn't support nvarchar(max) at all?

﹥>﹥吖頭↗ 提交于 2019-11-27 01:33:30
问题 I've spent decent amount of time on this problem and still can't figure out why EF team makes the life so hard using Code First. So here is some sample: My POCO: The way I want the thing to look like: public class Post { public int Id {get; set;} public string Text {get; set;} } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Post>() .Property(p => p.Text) .HasColumnType("nvarchar(max)"); } The only thing that works: public class Post { public int Id

EF - The context cannot be used while the model is being created exception during HTTP requests

亡梦爱人 提交于 2019-11-27 01:25:30
问题 I am receiving "The context cannot be used while the model is being created." issue in my web application in one of my webpages. This particular webpage POSTs to the server every 2-3 seconds to refresh the screen. From my testing I found that If I have 2 or more browser instances open to this page, after several minutes I receive a "The context cannot be used while the model is being created" exception from deep in the repository. This code calls a "service" to retrieve the needed data. This

An error occurred while saving entities that do not expose foreign key properties for their relationships

社会主义新天地 提交于 2019-11-27 00:41:33
问题 I have a simply code in Entity Framework 4.1 code first: PasmISOContext db = new PasmISOContext(); var user = new User(); user.CreationDate = DateTime.Now; user.LastActivityDate = DateTime.Now; user.LastLoginDate = DateTime.Now; db.Users.Add(user); db.SaveChanges(); user.Avatar = new Avatar() { Link = new Uri("http://myUrl/%2E%2E/%2E%2E") }; db.SaveChanges(); db.Users.Add(new User() { Avatar = new Avatar() { Link = new Uri("http://myUrl/%2E%2E/%2E%2E") } }); db.SaveChanges(); The problem is

Entity Framework Code First - Why can't I update complex properties this way?

痴心易碎 提交于 2019-11-27 00:27:43
问题 I'm working on a small sample project using Entity Framework 4.1 (code first). My classes look like this: public class Context : DbContext { public IDbSet<Person> People { get; set; } public IDbSet<EmployeeType> EmployeeTypes { get; set; } } public class Person { [Key] public int Key { get; set; } public string FirstName { get; set; } public string LastName { get; set; } virtual public EmployeeType EmployeeType { get; set; } } public class EmployeeType { [Key] public int Key { get; set; }

Can I use Data Annotations to perform a Cascade Delete with Entity Framework 4.1 RC?

微笑、不失礼 提交于 2019-11-27 00:27:27
问题 When using data annotations with EF4.1 RC is there an annotation to cause cascade deletes? public class Category { public int Id { get; set; } [Required] public string Name { get; set; } public ICollection<Product> Products { get; set; } } public class Product { public int Id { get; set; } public string Name { get; set; } public Category Category { get; set; } } Using this model the constraint generated is: ALTER TABLE [Product] ADD CONSTRAINT [Product_Category] FOREIGN KEY ([Category_Id])

Entity Framework 4.1 InverseProperty Attribute

 ̄綄美尐妖づ 提交于 2019-11-27 00:26:32
Just wanted to know more about RelatedTo attribute and I found out it has been replaced by ForeignKey and InverseProperty attributes in EF 4.1 RC. Does anyone know any useful resources about the scenarios that this attribute becomes useful? Should I use this attribute on navigation properties? example: public class Book { public int ID {get; set;} public string Title {get; set;} [ForeignKey("FK_AuthorID")] public Author Author {get; set;} } public class Author { public int ID {get; set;} public string Name {get; set;} // Should I use InverseProperty on the following property? public virtual