entity-framework-4.1

MVC3 and EF Data first: what are the best practices?

邮差的信 提交于 2019-12-03 06:18:55
问题 It seems that most of the focus with MVC3 and EF4.1 is around "code first" - I can't seem to find any examples or tutorials that meet the following criteria: uses an existing SQLServer database has separate projects for web & data access (we will have multiple web apps sharing the same data access classes) recommendations for validation Does such an example or tutorial exist? Are there any documented "best practices" for how to accomplish this, or rationale for NOT having a solution

Entity framework 4 - custom complex type mapping

末鹿安然 提交于 2019-12-03 06:07:06
I have a poorly written legacy database schema that I'm working with via EF Code First. I'm currently mapping POCO entities and would like to create an "Address" complex type and use this everywhere where street address information is stored. Unfortunately, not all of the address fields are named the same in the database (ie. one table might have "Address1" while another table will have "Street1" even though they refer to the same thing. Is there a way to create custom mappings for a complex type based on a given entity? What does that mapping look like? Yes, you can achieve that with fluent

in Entity framework, how to call a method on Entity before saving

扶醉桌前 提交于 2019-12-03 06:07:04
问题 Below I have created a demo entity to demonstrate what I'm looking for: public class User : IValidatableObject { public string Name { get; set; } [Required] public DateTime CreationDate { get; set; } public DateTime UpdatedOnDate { get; set; } public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { if(Name = "abc") { yield return new ValidationResult("please choose any other name then abc", new[] { "Name" }); } } } I am implementing IValidatableObject interface to

Entity Framework 4.1 for large number of tables (715)

*爱你&永不变心* 提交于 2019-12-03 06:06:46
I'm developing a data access layer for a database with over 700 tables. I created the model including all the tables, which generated a huge model. I then changed the model to use DBContext from 4.1 which seemed to improve how it compiled and worked. The designer didnt seem to work at all. I then created a test app which just added two records to the table, but the processor went 100% in the db.SaveChanges method. Being a black box it was difficult to accertain what went wrong. So my questions are Is the entity framework the best approach to a large database If so, should the model be broken

Best Practices for Lookup Tables in EF Code-First

旧城冷巷雨未停 提交于 2019-12-03 06:05:01
问题 I'm doing my first project with EF and I'm planning to go the code-first model. I'm trying to find a bit of guidance about handling a fairly classic "lookup table" scenario. I'm dealing with a pretty canonical situation where I'll be persisting address data. So, I have a simple address DTO... public class Address { public int Id { get; set; } public virtual string StreetAddress1 { get; set; } public virtual string StreetAddress2 { get; set; } public virtual string City { get; set; } public

EF 4.1 Code First and Existing Database and .NET Membership

你说的曾经没有我的故事 提交于 2019-12-03 05:58:32
问题 I have a database called ApplicationName_Development running on SQL Server 2008 R2 Developer edition on my development box. I added .NET membership tables to the database with no problem. When I tried to get Code First working I received the following error message: The server encountered an error processing the request. The exception message is "Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to

How to define relationships programmatically in Entity Framework 4.1's Code-First Fluent API

北战南征 提交于 2019-12-03 05:57:02
问题 I'm playing around with the new EF4.1 unicorn love. I'm trying to understand the different ways I can use code-first to programatically define my relationships between a few simple POCO's. How can I define the following => 1 Team has 0-many User s. (and a User is in 1 Team ) 1 User has 0-or-1 Foo 's (but a Foo has no property going back to a User ) 1 User has 1 UserStuff 回答1: Here you have examples you are looking for: public class User { public int Id { get; set; } ... public Foo Foo { get;

Entity Framework 4.1: How do I delete by object Id

人盡茶涼 提交于 2019-12-03 05:35:56
问题 I would like to know how to delete an object from Entity Framework 4.1 without first having to load the object from the database. I have found these other 2 answers on Stack Overflow, but they do not pertain to EF 4.1 I have tried the following code but it does not work public void DeleteCar(int carId) { var car = new Car() { Id = carId }; _dbContext.Cars.Attach(car); _dbContext.Cars.Remove(car); _dbContext.SaveChanges(); } I want to avoid the code below. public void DeleteCar(int carId) {

Partially updating object with EF Code First and ASP.NET MVC

微笑、不失礼 提交于 2019-12-03 04:53:18
EF4.1-Code-First-Gurus! I wonder if there is a more elegant way to handle the following ASP.NET MVC 3 EF 4.1 Code First scenario: Lets say we have the following POCOs: public class Entity { [Key] public int Id { get; set; } [ScaffoldColumn(false)] public DateTime CreatedOn { get; set; } [ScaffoldColumn(false)] public DateTime ModifiedOn { get; set; } } and public class Person : Entity { public string FirstName { get; set; } public string LastName { get; set; } public DateTime Birthday { get; set; } } Lets assume we have created some standard editing views, which are not including CreatedOn

Get Record ID in Entity Framework after insert

假如想象 提交于 2019-12-03 04:43:52
I'm developing an ASP.net application using Entity Framework. I'm using DetailsView to insert data into database. There is a table as Client and its primary key is client_id . client_id is auto generated by database. I need to get auto generated client_id after inserting a record into Client table and assign it to a hidden field for future use. I searched about this and I found lot of solutions. But I don't know how to use them since I'm new to asp.net. I found that Entity Framework automatically populates business objects with the db-generated values after call SaveChanges() . My question is