entity-framework-4

Entity Framework Code First and Connection Strings

泄露秘密 提交于 2019-12-10 14:14:37
问题 I have a small MVC 3 app using Entity Framework Code First and use this connection string for the model: data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Journal.mdf;User Instance=true;Database=MyJournal When I make a change to the model (e.g. add a property), I get as expected The model backing the 'JournalContext' context has changed since the database was created. So, being in development mode, I go ahead and delete Journal.mdf and Journal.ldf. Now when I

Specifying location of .csdl / .ssdl / .msl Metadata files within the output assembly

一笑奈何 提交于 2019-12-10 14:14:35
问题 I have an EF project that containing my data model that I have been successfully using. The "Metadata Artifact Processing" option is set to "Embed in Output Assembly". As the .edmx file was in the root folder of the project the metadata string used in the EntityConnectionStringBuilder was set to: res://*/myProject.csdl|res://*/myProject.ssdl|res://*/myProject.msl When I was restructuring the project, I moved the .ecdm file into a subfolder: /DataLayer/myProject/ and I changed the metadata

How to obtain the identity of an entity after calling SaveChanges() when the entity is mapped to stored procedures

若如初见. 提交于 2019-12-10 13:57:05
问题 We are using Entity Framework 4.0 and we have an entity that is mapped to stored procedures provided by our DBA. The Insert, Update, and Delete functions in the mapping details all have their own stored procedures. When using entities that are mapped to tables I am able to add a new entity, call dataContext.SaveChanges(); and then the new entity I instantiated automatically has its ID property populated with the value from the identity column in the database. How can this be accomplished when

How to get the latest date inserted from the table with Linq to entities

馋奶兔 提交于 2019-12-10 13:47:14
问题 how to retrieve the latestdate inspected from a column In our app a user can inspect an item and when it inspected the current date is entered into the table. For the same item someother can uninspect it this time nothing is inserted into the table. Another user can inspect it and current date is inserted I need to get the LatestDate Inserted..how to get this with Linq Let say table name is Statuses and col name is LastdateInspected 回答1: Sounds like you want something like: var query = db

EF4 code-first vs model-first with regards to model validation

给你一囗甜甜゛ 提交于 2019-12-10 13:38:01
问题 I'm working on a one-man ASP.NET MVC 3 project (I have complete control over database schema and code), and I'm trying to decide between going database-first and POCO w/ my EF4 models, or if I should go w/ code-first. The main thing I'm trying to achieve is decorating my models with DataAnnotation attributes so that I can enforce schema validation prior to doing any persistence. Looking at Scott Guthrie's article about model validation w/ MVC 2, he talks about article about doing it with code

Use a struct in place of a primitive for a EF4 property type

南笙酒味 提交于 2019-12-10 13:33:48
问题 I've got an EF4 entity (code-first) that includes an int bitmask. I've created a Bitmask struct to make working with bitmasks easier (provides bool properties to access the bits). The bitmask struct includes overloaded implicit operators for converting to and from an int. I tried setting the property type to the bitmask struct but the value is coming back as 0. I know the value in the database has a value and the bitmask works in my unit tests. I set the HasColumnType to "INT". The property..

Using .Include() when joining a view using Entity Framework [duplicate]

妖精的绣舞 提交于 2019-12-10 13:25:16
问题 This question already has answers here : Why doesn't Include have any effect? (2 answers) Closed last year . Im using an Indexed View which is an entity without any relations (relations to my table entities would be preferable but this seemed to be nearly impossible to achieve). This view consists of 4 FK's which together form the PK: PortalID CategoryID BranchID CompanyID Now im joining this view to select a set of companies like this: var companies = (from c in database.Companies.FindAll()

Using navigation properties in entity framework code first

你。 提交于 2019-12-10 13:13:39
问题 Context: Code First, Entity Framework 4.3.1; User ---- Topic, 1 to Many relation; User with public virtual ICollection<Topic> CreatedTopics Navigation Property(Lazy Loading); Topic with public virtual User Creator Navigation Property; DataServiceController : DbDataController<DefaultDbContext> , Web API beta, ASP.NET MVC 4 Beta , Single Page Application; System.Json for Json serialization; Web API Action: public IQueryable<Topic> GetTopics() { // return DbContext.Topics; // OK return DbContext

How to perform an update using ObjectContext

帅比萌擦擦* 提交于 2019-12-10 12:23:46
问题 I am using EF4 in my ASP.NET MVC 2 web application. I am attempting to save data to a database. It is fairly straightforward to add new data to a database. I'm doing it something like this: PeopleEntities entities = ObjectContextFactory.GetPeopleEntities(); entities.People.AddObject(person); // The Person object created from user input. entities.SaveChanges(); However, this has the unfortunate side-effect of always saving a new object to my database. I do need this functionality (of course),

Entity Framework and ObjectContext n-tier architecture

可紊 提交于 2019-12-10 12:18:41
问题 I have a n-tier application based on pretty classic different layers: User Interface, Services (WCF), Business Logic and Data Access. Database (Sql Server) is obviously quered throught Entity Framework, the problem is basically that every call starts from user interface and go throught all the layers, but doing that I need to create a new ObjectContext each time for every operation and that makes performance very bad because every time I need to reload metadata and recompile the query. The