entity-framework-4.1

Entity Framework Code First - How to ignore a column when saving

折月煮酒 提交于 2019-12-05 10:56:53
I have a class called Client mapped to a database table using Entity Framework code first. The table has a computed field that I need available in my Client class, but I understand that it won't be possible to write to this field. Is there a way of configuring Entity Framework to ignore the property when saving, but include the property when reading? I have tried using the Ignore method in my configuration class, or using the [NotMapped] attribute, but these prevent the property from being read from the database. You can use DatabaseGeneratedAttribute with DatabaseGeneratedOption.Computed

How to observe the Add action of DbSet<T>?

对着背影说爱祢 提交于 2019-12-05 10:29:24
I has two classes named Contact and ContactField as following. When the ContactField is added into Contact , I hope to assign SortOrder to ContactField automatically. Do I need to inherit DbSet and customize the Add method ? How to achieve it ? public class Foo { private MyDbContext _db = new MyDbContext(); public void HelloWorld() { Contact contact = ....; //< A contact from database. ContactField field = ....; ///< A new field .... ///< assign other properties into this `field` field.FieldType = FieldType.Phone; // How to automatically update `SortOrder` // when adding field into

Execute StoredProcedure in CodeFirst 4.1

巧了我就是萌 提交于 2019-12-05 10:02:29
I understand stored procedures mapping is not supported by my understanding is that I should be able to call stored procedures. I have quite a few complex stored procedures and with the designer I could create a complex type and I was all good. Now in code first let's suppose I have the following stored procedure, just put together something silly to give an idea. I want to return a student with 1 address. In code I have A Student and Address Entity. But no StudentAddressEntity as it's a link table. I have tried the following but I get an error Incorrect syntax near '."} System.Data.Common

How to correctly override the SaveChanges function in EF 4.1

旧时模样 提交于 2019-12-05 09:37:25
I need to automatically insert the current date when I create a new order in my EF 4.1 MVC3 web application. I also need to automatically add the currently logged in user - I'm using forms authentication. After searching the web, I found that I need to override the SaveChanges() function and an article on StackOverflow showed how to do this: Entity Framework 4.1 Automatic date . However, I couldn't get the code to work. Where does this code go? How do I find out what is? Sorry if the question is trivial - I'm new to EF. B. You can override in your context class, public partial class

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

北战南征 提交于 2019-12-05 08:48:24
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 ManyToManyConvention, but that means I must manually perform deletes for every join table in the system, which is

Cascading dropdown lists MVC3

落爺英雄遲暮 提交于 2019-12-05 07:27:08
问题 Relatively new to MVC and trying to get a cascading dropdown list working for train times. After looking at a lot of posts, people say that you should stay away from ViewBag/ViewData and instead focus on ViewModels, but I just can't seem to get my head round it, and it's driving me up the wall. Any tutorial seems to be either to complex or too easy and the whole viewModel idea just hasn't clicked with me yet. So here is my scenario: I have an admin system where staff can add individual train

Many to Many mapping not working - EF 4.1 RC

断了今生、忘了曾经 提交于 2019-12-05 06:39:39
问题 UPDATE: After a bit more research it seems a number of my many-to-many mappings aren't working. Hmmm... I'm upgrading a data access project from EF 4.1 CTP4 to EF 4.1 RC and I'm having trouble with the new EntityTypeConfiguration<T> setup. Specifically I'm having an issue with a Many-to-Many relationship. I'm getting a Sequence contains no elements exception when I'm trying to get the .First() item. The particular exception isn't really that interesting. All it's saying is that there are no

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

落花浮王杯 提交于 2019-12-05 05:47:53
问题 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

Can Entity Framework 4.1 designer “update model from database” for selected entities only?

倾然丶 夕夏残阳落幕 提交于 2019-12-05 05:46:39
The situation: Sometimes a database schema is not what you would consider an ideal representation of the system's information and you may not be able to change it. We have been using Entity Framework to create a nicer conceptual model to code against in situations like this. This means updating the model from the database and then changing it ourselves, either through the designer or through the .edmx file directly using a text editor. The problem: When you update the model from the database, all your carefully made changes are thrown out the window. This can make adding new entities a real

literal or constant as part of composite key in EF code first

左心房为你撑大大i 提交于 2019-12-05 05:43:08
I am relatively new to the Code First approach to Entity Framework. I have used the Database First approach for a while now, but the Code First seems to be a better fit for the application I am currently developing. I am working with an existing MS SQL database, and I am not allowed to make any changes whatsoever to the database. The reason why I am using Code First is because the Fluent API allows me to dynamically assign a table name to a class. That said, I have a predicament where I need to assign a relationship between 2 tables. One table, ArCodes, has a composite key made up of the