code-first

Many-To-Many Relationship in Code-First EF4

偶尔善良 提交于 2019-11-30 05:12:47
How do you represent a many-to-many relationship in the EF4 Code-First CTP3? For example if I have the following classes: class User { public int Id { get; set; } public string Name { get; set; } public ICollection<Profile> Profiles { get; set; } } class Profile { public int Id { get; set; } public string Name { get; set; } } In the database there is a UserProfiles table that has the FK for User and FK for Profile. How can I map this? EDIT: I understand how to to currently map with having a ICollection<User> property on the Profile , but I really don't want to have a an opposite navigation

Force EF 4.1 Code First to See an Attached entity as Modified

故事扮演 提交于 2019-11-30 04:54:54
All the examples I've found refer to a class called ObjectContext, which doesn't appear to exist in CTP5. I must stress at this point, CTP5 is my first exposure to the Entity Framework. I have a disconnected POCO that I have attached to my DbContext. SaveChanges does not pick up the change though, how I tell my context to update that entity? _context.Users.Attach(user); // The user has been replaced. _context.SaveChanges(); // The change is not saved. What am I doing wrong? Update 12/01/2011 Might be obvious to most, but as a first time user of EF, it didn't occur to me that attaching an

Code First vs. Database First

强颜欢笑 提交于 2019-11-30 04:46:44
I created an Entity Framework model based on an existing database, then generated the POCO entities from the model. The connection string in my web.config isn't Entity Framework, it's just the standard connection string (it's missing the CSDL, SSDL, MSL references). I can compile my application, but when I run I get this error: Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of

How do I map TimeSpan with greater than 24 hours to SQL server Code First?

ε祈祈猫儿з 提交于 2019-11-30 04:41:22
I am trying to map a TimeSpan Code First property to SQL server. Code First seems to be creating it as a Time(7) in SQL. However TimeSpan in .Net can handle longer periods than 24 hours and I need to store longer than 24 hour for event length. What is the best way to handle this with Code First. As per my previous question on how to store TimeSpan in SQL I was advised to store it as seconds or tickets etc. In the end I didn't map the TimeSpan column as there is no equivalent in SQL server. I simply created a 2nd field which converted the TimeSpan to ticks and stored that in the DB. I then

How do you actually perform relationships in Entity Framework 4 Code-First CTP 5?

匆匆过客 提交于 2019-11-30 04:03:41
I would like to have a short example on how do you actually perform relationships in Entity Framework 4 Code-First CTP 5 ? Would love an example for these kind of relations : * one-to-many * many-to-many Thanks a lots! WestDiscGolf One to One public class One { public int Id {get;set;} public virtual Two RelationTwo {get;set;} } public class Two { public int Id {get;set;} public virtual One RelationOne {get;set;} } Things to note, it has to be virtual One to Many public class One { public int Id {get;set;} public virtual ICollection<Two> RelationTwo {get;set;} } public class Two { public int

EF Code First Migration with Multiple Database / DbContext

点点圈 提交于 2019-11-30 04:00:41
I have two database each with their own dbcontext. I've setup two migration configurations. I can add a migration for the first db ust fine ( Add-Migration DB1_InitialCreate -ConfigurationTypeName DB1Configuration ). When I try to create an initial migration with the second db using: Add-Migration DB2_InitialCreate -ConfigurationTypeName DB2Configuration , I get the following error: Unable to generate an explicit migration because the following explicit migrations are pending: [201205082215265_DB1_InitialCreate]. Apply the pending explicit migrations before attempting to generate a new

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

Creating BiDirectional One - One relationship in Entity Framework 4.1 Code First

有些话、适合烂在心里 提交于 2019-11-30 02:45:02
I want to created Bi-Directional One-One relationship between two entities using EF Code First. I have trouble with the following code. What do you think I should do? public class User { public string ID { get; set; } public string LastName { get; set; } public string Password { get; set; } public string FirstName { get; set; } public int ProfileID { get; set; } public Profile Profile { get; set; } } public class Profile { public int UserID { get; set; } public User User { get; set; } public int ProfileID { get; set; } public string ProfileName { get; set; } public DateTime CreateDate { get;

Filtering navigation properties in EF Code First

你说的曾经没有我的故事 提交于 2019-11-30 02:40:33
问题 I'm using Code First in EF. Let's say I have two entities: public class Farm { .... public virtual ICollection<Fruit> Fruits {get; set;} } public class Fruit { ... } My DbContext is something like this: public class MyDbContext : DbSet { .... private DbSet<Farm> FarmSet{get; set;} public IQueryable<Farm> Farms { get { return (from farm in FarmSet where farm.owner == myowner select farm); } } } I do this so that each user can only see his farms, and I don't have to call the Where on each query

EF code first: How to delete a row from an entity's Collection while following DDD?

筅森魡賤 提交于 2019-11-30 00:22:53
So here's the scenario: DDD states that you use a repository to get the aggregate root, then use that to add/remove to any collections it has. Adding is simple, you simple call .Add(Item item) on the Collection you wish to add to. A new row is added to the database when you save. However, deleting is different - calling .Remove(Item item) doesn't remove the item from the database, it simply removes the foreign key. So while, yes, it is technically no longer part of the collection anymore, it's still in the database. Reading around, the only solution is to delete it using the data context. But