entity-framework-ctp5

EF Code First - Include(x => x.Properties.Entity) a 1 : Many association

社会主义新天地 提交于 2019-11-30 06:29:34
问题 Given a EF-Code First CTP5 entity layout like: public class Person { ... } which has a collection of: public class Address { ... } which has a single association of: public class Mailbox { ... } I want to do: PersonQuery.Include(x => x.Addresses).Include("Addresses.Mailbox") WITHOUT using a magic string. I want to do it using a lambda expression. I am aware what I typed above will compile and will bring back all Persons matching the search criteria with their addresses and each addresses'

using Guid as PK with EF4 Code First

穿精又带淫゛_ 提交于 2019-11-30 06:20:33
问题 I have this class and table: public class Foo { public Guid Id {get;set;} public string Name {get;set;} } create table Foo ( id uniqueidentifier primary KEY DEFAULT (newsequentialid()), name nvarchar(255) ) the problem is that when i try to save new foo the first one goes with the 0000-000-00 ... id and the second also, so I get constraint exception anybody knows a fix ? 回答1: Have you set Identity StoreGeneratedPattern? You can do it in the OnModelCreating method: modelBuilder.Entity<Foo>()

how to do many to many with the same table with EF4 Code First

霸气de小男生 提交于 2019-11-30 03:09:23
问题 I have this schema: create table Person ( id int identity primary key, name nvarchar(30) ) create table PersonPersons ( PersonId references Person(id), ChildPersonId references Person(id) ) how to create the classes to map them using EF4 Code First CTP5 ? 回答1: For the POCO... class Person { public Guid PersonId { get; set; } public virtual Person Parent { get; set; } public virtual ICollection<Person> Children { get; set; } } ...set up the mapping in the DbContext... protected override void

How to join tables in EF LINQ

别说谁变了你拦得住时间么 提交于 2019-11-29 13:29:51
问题 When I try to join tables var query = from foo in db.Foos from bar in db.Bars where foo.ID == bar.FooID where foo.ID == 45 select bar; query.toArray() I get such error Unable to create a constant value of type 'Bar'. Only primitive types ('such as Int32, String, and Guid') are supported in this context. 回答1: Try that instead: var query = from foo in db.Foos join bar in db.Bars on foo.ID equals bar.FooID where foo.ID == 45 select bar; Anyway, I suggest you model the relation between Foo and

Entity Framework CTP5 Code-First Mapping - Foreign Key in same table

て烟熏妆下的殇ゞ 提交于 2019-11-29 11:16:35
How would I map something like this using the modelBuilder? Where theres a nullable foreign key referencing the same tables primary key Table: Task taskID int pk taskName varchar parentTaskID int (nullable) FK Task class: public class Task { public int taskID {get;set;} public string taskName {get;set;} public int parentTaskID {get;set;} public Task parentTask {get;set;} } ... modelBuilder.Entity<Task>() .HasOptional(o => o.ParentTask).... The following code gives you the desired schema. Note that you also need to define ParentTaskID foreign key as a nullable integer, like I did below. public

EF4 Independent Associations - Why avoid them?

 ̄綄美尐妖づ 提交于 2019-11-29 07:15:02
I saw this comment on MSDN ( link and link ): "Note that Independent Associations should often be avoided since things like N-Tier and concurrency becomes more difficult." I'm new to EF4 and I'm building an n-Tier web app. This sounds like an important pitfall. Can someone explain to me what this means? I think it's personal preference. Originally, EF was created to only use indep. associations and aligned with a more classic ERM approach. However, most of us devs are so dependent on FKs that it made life very complex. So MS gave us FKs in EF4 which meant not only having the FK as a property

Using Entity Framework Code First CTP5, how do I create a primary key column that are INTs and are not identity columns

你说的曾经没有我的故事 提交于 2019-11-29 05:25:36
Using Entity Framework Code First CTP5, how do I create a primary key column that are INTs and are not identity columns Preferably not using attributes. Matt Hamilton If you're building your entities using the fluent interface rather than using attributes over the properties, you may be able to use the DatabaseGenerationOption class (from EntityFramework.dll, in the System.ComponentModel.DataAnnotations namespace). I've not tested it, but this is what it would look like: modelBuilder .Entity<Foo>() .Property(f => f.Id) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); That answer didn

LINQ to Entities does not recognize the method

让人想犯罪 __ 提交于 2019-11-29 04:33:50
I am following this article on MSDN. I ported it to EF Code First. public interface IUnitOfWork { IRepository<Employee> Employees { get; } IRepository<TimeCard> TimeCards { get; } void Commit(); } public class HrContext : DbContext { public DbSet<Employee> Employees { get; set; } public DbSet<TimeCard> TimeCards { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Employee>() .HasMany(e => e.TimeCards) .WithOptional(tc => tc.Employee); } } public class SqlRepository<T> : IRepository<T> where T : class { private readonly DbSet<T> entitySet;

Problem with Eager Loading Nested Navigation Based on Abstract Entity (EF CTP5)

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 00:26:08
I a portion of my EF model that looks like this: Summary: Location has many Posts Post is an abstract class Discussion derives from Post Discussions have many Comments Now, the query i'm trying to achieve: Get information about Location Id 1234, including any Discussions and Comments associated with those Discussions. I can get discussions and the comments like this: var discussions = ctx.Posts .OfType<Discussion>() .Include(x => x.Comments) .ToList(); But i can't seem to get it based on the Posts navigation on the Location entity. I've tried this: var locationWithDiscussionsAndComments = ctx

Entity Framework 4 CTP 5 Self Referencing Many-to-Many

巧了我就是萌 提交于 2019-11-28 23:54:03
I have the following scenario in my database. It is a record of Studies and those studies have other studies as prerequisites. In my DB design, it looks like this: And my code looks something like this: public class Study { public int ID { get; set; } public string Topic { get; set; } public byte TypeID { get; set; } public virtual StudyType Type { get; set; } public bool Deprecated { get; set; } public virtual ICollection<Study> Prerequisites { get; set; } } public class StudyType { public byte ID { get; set; } public string Name { get; set; } public virtual ICollection<Study> Studies { get;