entity-framework-4

EF 4.0 IsAttachedTo extension method and error An object with the same key already exists

跟風遠走 提交于 2019-12-31 07:32:46
问题 I was getting an error An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key. After i googled it i found IsAttachedTo extension method there: Is is possible to check if an object is already attached to a data context in Entity Framework? here is my code: foreach (string s in types) { Subscription subscription = new Subscription { Id = Int32.Parse(s) }; if (service.repository._context.IsAttachedTo

EF4 Code First - How to properly map Splitting an Entity Across Multiple Tables

帅比萌擦擦* 提交于 2019-12-30 09:39:15
问题 I am using EF4 CTP5 to try to persist a POCO object that is split among two tables, the link being the ContactID. When i save a contact, i want the core contact information saved in one table (Contacts), and the link to the user who owns the contact saved in another (UserToContacts). I have the custom mapping defined below, but when I SaveChanges, i get the following error: A value shared across entities or associations is generated in more than one location. Check that mapping does not split

Entity Framework Is it possible to add an ASSOCIATION between Primary Keys and a Foreign Key

北城以北 提交于 2019-12-30 09:06:09
问题 I've got the following entities on my EDMX :- These two entites were generated by Update Model From Database . Now, notice how my country has the following primary key :- Name & IsoCode this is because each country is UNIQUE in the system by Name and IsoCode. Now, with my States ... it's similar. Primary Key is :- Name & CountryId Each state is unique by name and per country. Now, the Foreign Key for States is a CountryId. This is the sql :- ALTER TABLE [dbo].[States] WITH CHECK ADD

A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'ID'

戏子无情 提交于 2019-12-30 07:56:34
问题 I can't figure out why I get this error when I try to add a Venue object and call SaveChanges(). The only difference in the model with Venue objects is they are 1 to 1..0 relation with City. City city = ProcessCityCache(ev, country, db); // After this call, 'city' is already persisted. venue = new Venue { TicketMasterURL = ev.VenueSeoLink, Name = Capitalize(ev.VenueName), City = city }; db.Venues.AddObject(venue); db.SaveChanges(); // Exception thrown here. Any insight would be greatly

Should the EnumDataTypeAttribute work correctly in .NET 4.0 using Entity Framework?

时光怂恿深爱的人放手 提交于 2019-12-30 07:24:22
问题 I have an enumeration which I'd like to persist as a value of some sort into the underlying database so that I can bring it back and forth. I have read some articles that suggest to create a enumeration wrapper with static implicit operators defined, mapped using a ComplexType object mapping as described in the link below. How to fake Enums in EF4 This solution works flawlessly! My thanks to Alex James. Aside, I discovered of the EnumDataTypeAttribute Class which purpose seems to handle enums

How to use Automapper to create complex viewmodel objects from a simple object and a method call

陌路散爱 提交于 2019-12-29 09:28:22
问题 My application needs to display a table of customer data, including data about the customer and about his most recent order from a given warehouse. The customer domain object contains a GetLatestOrder(warehouseId) method. I have a CustomerView viewmodel and want to be able to populate it with some fields from the customer object, and a few fields from the latest order object. Can I do this using Automapper? 回答1: Try this, In Global.asax.cs [ Application_Start ], public static void

EF4: ObjectContext Lifetime?

99封情书 提交于 2019-12-29 08:41:31
问题 I am developing a WPF desktop app that uses Entity Framework 4 and SQL Compact 4. I have seen two distinct styles of Repository classes: The Repository instantiates an ObjectContext , which is disposed of when the Repository is garbage-collected. The lifetime of the ObjectContext is the same as the lifetime of the application. A separate DataStoreManager class creates and holds an ObjectContext for the life of the application. When a repository is needed, a command gets an ObjectContext

MySQL Entity Framework 4.0 Stored Procedure Field Mapping

纵然是瞬间 提交于 2019-12-29 06:45:49
问题 Has anyone here used MySQL with the entity framework 4.0 and stored procedures? When I add a SP, it does not show any of my fields that I need to input. I also see no way to manually add them. When I click Function Import Mapping, it simply says "Select an Entity or Association on the Entity Designer Model Browser to edit it's mapping". Any help is appreciated. I am using the .NET Connector 6.3.6 回答1: due to the bug #55778 (Stored procedure parameters are omitted during update of the entity

Entity Framework 4 Multiple Object Delete(RemoveAll)

流过昼夜 提交于 2019-12-29 06:45:08
问题 I read that the new Entity Framework will include a method to delete multiple items (Linq to SQL has DeleteAllOnSubmit()) but I can't find the function/method to do that. Is this in Beta 2 or do I have to make my own? UPDATE: This is what I'm using now: public void DeleteObjects(IEnumerable<object> objects) { foreach (object o in objects) { DeleteObject(o); } SaveChanges(); } 回答1: EF 4 allows you to execute TSQL statements against an object context: using (var context = new

Connection string for using SQL Server Compact with Entity Framework?

限于喜欢 提交于 2019-12-29 06:14:21
问题 I'm done trying to Google for this. I have installed SQL Server CE 4.0, and have EF 4.1, but I can't get a proper connection string. Nothing on connectionstrings.com applies to me. I just want to create a SqlCeEngine object, but no matter what I try I get some exception. Most recently it's been Unknown connection option in connection string with either "metadata", "app", "provider", or "provider connection string" after it. I know EF requires metadata in the connection string. And I can't