entity-framework-4.1

Entity Framework Code First Casting Columns

六月ゝ 毕业季﹏ 提交于 2019-12-23 03:25:40
问题 I have an class public class Foo { public int UserId { get; set; } } and I'm using the code first fluent mapping to map this to the database. Property(i => i.UserId) .HasColumnName("userno"); the only problem is that userno is actually a char(10) in the database. How do I go about casting or converting this type? as I currently get this error. The 'UserId' property on 'Foo' could not be set to a 'String' value. You must set this property to a non-null value of type 'Int32'. Thanks 回答1: Entity

Updating a many-to-many relationship

£可爱£侵袭症+ 提交于 2019-12-23 03:04:58
问题 I've asked the question a few different times in a few different ways and I haven't yet gotten any responses. I'm trying again because I feel like my solution is too convoluted and I must be missing something simpler to do. Using EF 4.1, POCO, DbContext API, AutoMapper, and Razor in an MVC 3 application. I have a many-to-many relationship between two of my entities: Proposals and CategoryTags. I can successfully map (Automapper) a Proposal to my ProposalViewModel including the collection of

Dependency injection with EF DbContext that implements 2 interfaces

白昼怎懂夜的黑 提交于 2019-12-22 18:24:03
问题 Given a DbContext that implements 2 interfaces like so: public interface IQueryEntities { IQueryable<User> Users { get; } IQueryable<Computer> Computers { get; } // other IQueryable<T> get properties } public interface IUnitOfWork { int SaveChanges(); } public class MyContext : DbContext, IQueryEntities, IUnitOfWork { // implement interfaces using EF } First question, is it a bad idea to separate out the query aspects of DbContext (IDbSets) from the command aspects (SaveChanges)? I am

How to handle an association table linking the same 2 tables

蹲街弑〆低调 提交于 2019-12-22 18:12:55
问题 I am using Entity Framework 4.3 code first using an already existing database. There is a Users table that has the following columns: - UserID (int) - FirstName (string) - Surname (string) I have an association table called Impersonations . The Impersonations table is an association table between a user table and the same user table. A user can have a list of users. Here is the Impersonations table structure: - ImpersonateID (int primary key) - UserID (int FK to Users table's UserID column) -

Entity Framework Code First callback on object instantiation

冷暖自知 提交于 2019-12-22 11:13:00
问题 This may be a far fetched question, but is it possible to have a callback fire in an entity object, whenever a new instance of it has been loaded from the database (as part of e.g. a linq query), a call to Create or similar? The purpose of such a callback would be to convey a context, or set of initialization parameters, from the enclosing business object. 回答1: DbContext definitely doesn't have it but you can try to convert it back to ObjectContext and use: var objectContext = (

Model updates to contained collection not saved in DB when saving parent entity?

你说的曾经没有我的故事 提交于 2019-12-22 10:26:50
问题 Issue: Child model collection in MVC3 & Entity Framework 4.1 is being updated properly in model via Edit action but the values are not being saved in DB. Overview: - Model object Person contains object CaseRef's - Person property updates are getting saved in the DB on db.SaveChanges() but internal collection CaseRef property updates are not being saved - All values are bound/mapped correctly upon entry of HttpPost ActionResult Edit() so the model is being updated successfully from Form submit

DBContext, state and original values

天涯浪子 提交于 2019-12-22 08:43:27
问题 I'm working with ASP.NET MVC3 using EF and Code First. I'm writing a simple issue tracker for practice. In my Controller I have a fairly standard bit of code: [HttpPost] public ActionResult Edit(Issue issue) { if (ModelState.IsValid) { dbContext.Entry(issue).State = EntityState.Modified ..... } } Question part 1 I'm trying to get my head around how the dbcontext works - Before I've set the State on the dbContext.Entry(issue), I assume my issue object is detached. Once I set the state to be

EntityFramework 4.1 Code First incorrectly names complex type column names

非 Y 不嫁゛ 提交于 2019-12-22 08:42:55
问题 Say I have a table called Users, which contains your typical information: Id, Name, Street, City--much like in the example here: http://weblogs.asp.net/manavi/archive/2010/12/11/entity-association-mapping-with-code-first-part-1-one-to-one-associations.aspx. Among other things, this article states: "Code First has a concept of Complex Type Discovery that works based on a set of Conventions. The convention is that if Code First discovers a class where a primary key cannot be inferred, and no

multiple cascade delete path in many-many relationship (EF 4.1)

假如想象 提交于 2019-12-22 07:25:11
问题 THE TABLES: Shop Product Category THE RELATIONSHIPS: (Shop) 1 <---> n (Categories) (Shop) 1 <---> n (Products) (Categories) n <---> n (Products) THE CASCADE DELETES: Shop ---> Categories ... I defined this using fluent API Shop ---> Products ... I defined this using fluent API Categories <---> Products ... EF 4.1 automatically defines cascade for "Category_Product" join table THE PROBLEM: Above results in a "multiple" cascade deletion path exception. POTENTIAL FIXES: Remove the

ASP.NET MVC with EF 4.1 Navigation properties

喜欢而已 提交于 2019-12-22 06:16:35
问题 After days studying EF to understand (kinda..) how it works, I finally realized that I might have a big problem. Imagine that I have two entities: Pais and UF . The relationship between them is Pais (0..1) ... (*) UF . A screenshot: http://i.imgur.com/rSOFU.jpg. Said that, consider that I have a controller called UFController and it has actions for Edit and Create , which are just fine. My views are using the EditorFor helper (or similar ones) for inputs, so when I submit the form the