entity-framework-4.1

Entity Framework 4.1 - Code First with existing Database, how to define classes, using Attributes or EntityTypeConfiguration? What is the difference?

时光总嘲笑我的痴心妄想 提交于 2019-12-03 21:17:38
I have been studying the EF for a short time and cant find the answer to this question. I have existing database and I am using CodeFirst to create classes for the model. What is the difference in using Attributes and EntityTypeConfiguration to define parameters of table columns? Since the database already has defined foreign keys and unique constraints, and so on, how and where to implement the validation for a best and most fluid result for use in ASP.NET MVC3? Is it better to implement Attributes and CustomValidation or to use TryCatch blocks to catch errors from db? Does Validator

How can I use TPT inheritance models when primary keys have different names?

社会主义新天地 提交于 2019-12-03 20:31:22
问题 Using Entity Framework 4.1 against a legacy database, I'm unable to generate a working set of TPT inheritance models that aren't pluralized and use different names for a common primary key. I am using database tables Organization, Account, and Company as illustrated below: Organization OrganizationID (int PK) OrgName (varchar) Company CompanyID (int PK) CompanyNo (varchar) Account AccountID (int PK) AccountNo (varchar) Account.AccountID and Company.CompanyID have a FK constraint that values

Entity Framework Code First - One-to-Many with a join/link table

*爱你&永不变心* 提交于 2019-12-03 20:23:17
Is it possible to create a One-to-Many relationship with Code First that uses a link/join table between them? public class Foo { public int FooId { get; set; } // ... public int? BarId { get; set; } public virtual Bar Bar { get; set; } } public class Bar { public int BarId { get; set; } // ... public virtual ICollection<Foo> Foos { get; set; } } I want this to map as follows: TABLE Foo FooId INT PRIMARY KEY ... TABLE Bar BarId INT PRIMARY KEY TABLE FooBar FooId INT PRIMARY KEY / FOREIGN KEY BarId INT FOREIGN KEY With this, I would have the ability to make sure Foo only has one Bar, but that

Mapping association tables in EF 4.1 code first

ぃ、小莉子 提交于 2019-12-03 18:58:14
问题 I'm not sure how to map the following tables below in EF 4.1 code first and what objects I need representing the tables. How would I retrieve a list of the product's specifications? I currently only have a Product class. Products Table: Id Name IsActive ProductSpecification Table: ProductId SpecificationId Specifications Table: Id Name IsActive ProductSpecifications is an association table. I also have the following defined in my context class: public DbSet<Product> Products { get; set; }

unidirectional many-to-many relationship with Code First Entity Framework

試著忘記壹切 提交于 2019-12-03 18:57:40
问题 I am new to EF, and trying to get many-to-many unidirectional relationship with code first approach. For example, if I have following two classes (not my real model) with be a N * N relationship between them, but no navigation property from "Customer" side. public class User { public int UserId { get; set; } public string Email { get; set; } public ICollection TaggedCustomers { get; set; } } public class Customer { public int CustomerId { get; set; } public string FirstName { get; set; }

Entity framework 4 - custom complex type mapping

泄露秘密 提交于 2019-12-03 17:05:00
问题 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

Code first - how can I save an ICollection when I'm not setting the parent object to EntityState.Modified?

余生长醉 提交于 2019-12-03 16:30:04
If I have the below class: public class Foo() { public int PropertyIWantUpdated {get; set;} public int PropertyIDontWantUpdated (get; set} public ICollection<Bar> Bars {get; set;} } When saving to my db, instead of context.Entry(thisFoo).State = EntityState.Modified; I'm using context.Entry(thisFood).Property(tf => tf.PropertyIWantUpdated).IsModified = true; how can I also save changes to Bars? Ladislav Mrnka It depends what you are trying to update. First of all let me clarify one important fact - if you have detached entity graph (more entities with relation) and you want to pass all changes

DbEntityEntry.OriginalValues not populating complex properties

假装没事ソ 提交于 2019-12-03 16:11:12
I'm writing an audit trail from snippets of code found online. On the call to my SaveChanges function I loop through all the modified entities registered with the Context and build log entries from their changes. foreach (DbEntityEntry modifiedEntity in this.ChangeTracker.Entries().Where(p => p.State == System.Data.EntityState.Added || p.State == System.Data.EntityState.Deleted || p.State == System.Data.EntityState.Modified)) { // For each changed record, get the audit record entries and add them foreach(AuditLog x in GetAuditRecordsForChange(modifiedEntity, userId)) { this.AuditLog.Add(x); }

Keep Database Content On Model Change

╄→尐↘猪︶ㄣ 提交于 2019-12-03 16:09:03
Using the code-first approach available in the new 4.1 RC. Is there any way to persist the current data stored in a database when the mode changes? The database is created by the entity framework, and usually the database is dropped and recreated on model changes. Obviously as soon as the model is changed it will not be possible to use the context object to connect to the database to retrieve the data, so what are the options? Code first doesn't support database migration / evolution yet. If you want to do incremental DB development use model first (EDMX) with DbContext Generator T4 template

Entity Framework and sharded database

耗尽温柔 提交于 2019-12-03 15:58:16
I have a WCF Data Service running on top of a Entity Framework code first 4.1 provider. The database is quite large (one key table has 77+ million records and grows by ~10% per month) and has presented quite a performance problem. While sharding a database that large is a pain it seems inevitable. My question is, has anybody successfully implemented EF with a sharded database and, if so, do you have any guidance? Have you investigated the following options: Clustering your DB (I assume it's SQL Server you are using)? Extracting some of your data (archived records, for example) into another