code-first

Inheritance EF Code-First

蓝咒 提交于 2019-12-21 04:08:15
问题 I have a base object that I dont want to be mapped in DB as an entity, I only want the properties to be added to the object that is mapped in the DB : Not mapped object (dont know if it matters but baseobject is in another assembly): public class BaseObject { public virtual string Prop1 { get; set; } public virtual string Prop2 { get; set; } } Mapped object: public class ChildObject : BaseObject { public virtual string Prop3 { get; set; } public virtual string Prop4 { get; set; } public

How to update a row using Entity Framework code first?

我的未来我决定 提交于 2019-12-21 04:06:36
问题 How should I go about updating a row in the database? There is no update method, and if I use add and the primary key id already exists, I get an exception. Please provide an example if possible. 回答1: The easiest way is: (1) retrieve existing row using pk. (2) update properties. (3) call SaveChanges() on context. e.g. var student = context.Students.Find(42); student.Description = "updated"; context.SaveChanges(); 回答2: Here is a way that worked for me without having to make a query first:

Entity Framework 4 - Mapping non-public properties with CTP5 (code first) in a persistence unaware context

南笙酒味 提交于 2019-12-21 03:46:12
问题 I know the question already has a solution (eg. this question) but I really can't afford to attach the mapping logic in the same assembly where the domain (POCO classes) is. Is there any other way? I found this nice blog post but I couldn't get it working. Here is the model: public class Institute { /** Code omitted **/ protected virtual ICollection<InstituteText> InnerInstituteTexts { get; set; } private InstituteTextSet _TextSets; public InstituteTextSet Texts { get { if (_TextSets == null)

Entity Framework 4 - Mapping non-public properties with CTP5 (code first) in a persistence unaware context

烂漫一生 提交于 2019-12-21 03:46:03
问题 I know the question already has a solution (eg. this question) but I really can't afford to attach the mapping logic in the same assembly where the domain (POCO classes) is. Is there any other way? I found this nice blog post but I couldn't get it working. Here is the model: public class Institute { /** Code omitted **/ protected virtual ICollection<InstituteText> InnerInstituteTexts { get; set; } private InstituteTextSet _TextSets; public InstituteTextSet Texts { get { if (_TextSets == null)

EF4 Code First, TDD, CRUD, and Transactions

故事扮演 提交于 2019-12-21 02:56:25
问题 In the past, I've written unit tests for simple CRUD operations when creating data access/repository code that look something like this: using(var connection = new WhateverConnection(connectionString)) { connection.Open(); using(var transaction = connection.BeginTransaction()) { try { //test the CRUD operation } finally { //gets rid of any stuff created during the test transaction.Rollback(); } } } I was messing around with EF4 Code First today, and I realized that I have no idea how this

How do I get the raw SQL underlying a LINQ query when using Entity Framework CTP 5 “code only”?

纵饮孤独 提交于 2019-12-20 17:48:09
问题 I've using Entity Framework CTP5 in "code only" mode. I'm running a LINQ query on a object that was return from the database, as the query is running really slowly. Is there any way in which I can get the SQL statement that is being generated from the query? Topic currentTopic = (from x in Repository.Topics let isCurrent = (x.StoppedAt <= x.StartedAt || (x.StartedAt >= currentTopicsStartedAtOrAfter)) where x.Meeting.Manager.User.Id == user.Id && isCurrent orderby x.StartedAt descending select

How do I get the raw SQL underlying a LINQ query when using Entity Framework CTP 5 “code only”?

若如初见. 提交于 2019-12-20 17:48:09
问题 I've using Entity Framework CTP5 in "code only" mode. I'm running a LINQ query on a object that was return from the database, as the query is running really slowly. Is there any way in which I can get the SQL statement that is being generated from the query? Topic currentTopic = (from x in Repository.Topics let isCurrent = (x.StoppedAt <= x.StartedAt || (x.StartedAt >= currentTopicsStartedAtOrAfter)) where x.Meeting.Manager.User.Id == user.Id && isCurrent orderby x.StartedAt descending select

Deploy Entity Framework Code First

…衆ロ難τιáo~ 提交于 2019-12-20 12:04:04
问题 I guess I should have thought of this before I started my project but I have successfully built and tested a mini application using the code-first approach and I am ready to deploy it to a production web server. I have moved the folder to my staging server and everything works well. I am just curious if there is a suggested deployment strategy? If I make a change to the application I don't want to lose all the data if the application is restarted. Should I just generate the DB scripts from

how to annotate a parent-child relationship with Code-First

十年热恋 提交于 2019-12-20 09:36:36
问题 When using the CTP 5 of Entity Framework code-first library (as announced here) I'm trying to create a class that maps to a very simple hierarchy table. Here's the SQL that builds the table: CREATE TABLE [dbo].[People] ( Id uniqueidentifier not null primary key rowguidcol, Name nvarchar(50) not null, Parent uniqueidentifier null ) ALTER TABLE [dbo].[People] ADD CONSTRAINT [ParentOfPerson] FOREIGN KEY (Parent) REFERENCES People (Id) Here's the code that I would hope to have automatically

How to have multiple one-to-many relations between two entities using Entity Framework Code First

若如初见. 提交于 2019-12-20 06:18:14
问题 Below is a simple approach to save relational database records which is working perfectly fine. I have doubt on one scenario. Before that i need to know the way i am approaching has any difficulties if the database complexity increases. Any better, efficient but simple approach? ONE to ONE: tb_student // store student details id, name, country_id (country_id foriegnkey set with id of tb_country) tb_country // store all available countries id, name [Table("tb_student")] public class Student {