entity-framework-5

How to get list of modified objects in Entity Framework 5

泄露秘密 提交于 2019-11-29 02:02:31
问题 I'm binding list of entities to a data grid view like this: var orders = context.Order.ToList(); BindingList<Order> orderList = new BindingList<Order>(orders); dataGridView1.DataSource = orderList; User can edit or add new directly on datagridview. When user click Save button, in order to optimize performance, I want to retrieve list of entities that has been changed/new to perform insert/update. How can I achieve this? EDIT Define add new row to gridview: BindinList<Order> orders =

A relationship is in the Deleted state

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 01:43:05
问题 When I am trying to clear a collection (calling .Clear ) I get the following exception: An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details. The inner exception is: A

How to use a dynamic connection string using a Model First approach but still use the data model in the EDMX?

非 Y 不嫁゛ 提交于 2019-11-29 01:42:20
问题 I have created an EDMX using EF 5 with the Model First approach, i.e. I started with a blank designer and modeled my entities. Now I want to be able to use this model defined in the EDMX but supply runtime SQL Server connection strings without modyfing the config file. I know how to pass a connection string to the DbContext but the issue is locating the metadata for the mappings within the assembly. For example, my EDMX has this connection string in the app.config <add name="MesSystemEntities

“Execute Code First Migrations” checkbox disappeared from my publish profile

只愿长相守 提交于 2019-11-29 01:38:06
I'm using web deploy to deploy an MVC4 application using EF5 code first. I made a publish profile called "development" that uses web deploy for application and database using the 'Execute Code First Migrations' checkbox to run migrations on application startup. The publishing worked great for a while. At some point I added a new publish profile called "test" to deploy to another server, which uses the ftp method of deploy and no automatic migrations. This works fine too. However, when I tried to use my old "development" publish profile again, VS changes the settings automatically to the

Best way to insert data to multiple table MVC ASP

早过忘川 提交于 2019-11-29 01:29:44
问题 I have 4 tables. OperationTable, ClientTable, ClientDetails, OperationRes ClientTable ClientID Name Surname Birthday VerNumber ClientDetails ClientID Email Adress Telephone OperationTable OperationID Date Time ClientID OperationRes ResID OperationID Name Type No i have page where we ask Client to fill a form to register for smth. Everything must be in one page and after Client submit the form, we must insert all data to it's table. Date and Time to OperationTable, Name and Surname to

Entity Framework - getting a table's column names as a string array

时间秒杀一切 提交于 2019-11-29 00:15:08
问题 If I'm using EF 5 and Database first to generate a .edmx model of my database, how do I get a list of an entity's columns? using (var db = new ProjectNameContext()) { // string[] colNames = db.Users. } What I'm looking for is colNames[0] == "Id", colNames[1] == "FirstName", etc. 回答1: How about: var names = typeof(User).GetProperties() .Select(property => property.Name) .ToArray(); Of course, this can be used for any type, not just an EF table. 回答2: var res = typeof(TableName).GetProperties()

Unable to sort with property name in LINQ OrderBy

微笑、不失礼 提交于 2019-11-28 21:35:46
Error is LINQ to Entities does not recognize the method 'System.Object GetValue(System.Object, System.Object[])' method, and this method cannot be translated into a store expression. My code is public static GridResult GetAllUsers(int count, int tblsize,string sortcreteria) { using (UserEntities entity = new UserEntities()) { var data = entity.User_Details.Take(count) .OrderBy(i =>.GetType().GetProperty(sortcreteria).GetValue(i,null)) .Skip(tblsize).ToList(); result.DataSource = data; result.Count = entity.User_Details.Count(); } return result; } How to sort with property name as string? Ziad

Entity Framework 5 - How to generate POCO classes from existing database

喜夏-厌秋 提交于 2019-11-28 21:26:49
I am using VS 2012 and EF 5. I have an existing database that I want to create POCO classes from the existing database. I followed the steps to add an ADO.NET Entity Data Model to my project. I went through the wizard to use an existing database. It then created the edmx and tt files with the designer open. However, I want to create the POCO objects and use them. The Microsoft site states that the POCO Entity Framework Generator is obsolete and I should use the DBContext Generator. I can't figure out steps I use to generate the POCO classes. I only see the edmx designer. I really don't even

Can I get decode an EntityFramework Model from a specified migration?

守給你的承諾、 提交于 2019-11-28 21:23:45
Apparently IMigrationMetadata.Target encodes the state of the EF model. Can I use this to reconstruct the model for a particular migration? Yes, it is possible. I was myself curious what exactly those magic resource strings were storing. By digging into the Entity Framework source (see the DbMigrator.GetLastModel() method), I found out that the IMigrationMetadata.Target just stores a base-64 string containing gzipped XML data. To test this, I created a new console application containing a simple code-first model defined as follows: public class ContactContext : DbContext { public virtual

How to use an existing enum with Entity Framework DB First

ぃ、小莉子 提交于 2019-11-28 21:04:59
问题 I am using Entity Framework 5, DB first. I know how to define an enum on my model, and set the type of a field to that enum. Now, I have a requirement to map a field MyField to an enum that is defined externally, i.e. not in the EF model ( OtherNamespace.MyEnum ). The designer does not allow me to set the type to anything outside the model. I tried editing the edmx file manually, but that causes an error: Error 10016: Error resolving item 'MyField'. The exception message is: 'Unresolved