entity-framework-4.1

Atomic Increment with Entity Framework

偶尔善良 提交于 2019-11-28 09:49:23
I have a MySQL Server which I access using Entity Framework 4.0. In the database I have a table called Works into which some counts. I develop web site with Asp.net. This table acccesable one more user same time. And this situation causes wrong incerement problem. My code like that: dbEntities myEntity = new dbEntities(); var currentWork = myEntity.works.Where(xXx => xXx.RID == 208).FirstOrDefault(); Console.WriteLine("Access work"); if (currentWork != null) { Console.WriteLine("Access is not null"); currentWork.WordCount += 5;//Default WordCount is 0 Console.WriteLine("Count changed");

Model binding in the controller when form is posted - navigation properties are not loaded automatically

感情迁移 提交于 2019-11-28 09:44:16
问题 I'm using the Entity Framework version 4.2. There are two classes in my small test app: public class TestParent { public int TestParentID { get; set; } public string Name { get; set; } public string Comment { get; set; } public virtual ICollection<TestChild> TestChildren { get; set; } } public class TestChild { public int TestChildID { get; set; } public int TestParentID { get; set; } public string Name { get; set; } public string Comment { get; set; } public virtual TestParent TestParent {

EF 4.1 Code First error - The entity type SomeType is not part of the model for the current context

寵の児 提交于 2019-11-28 09:35:28
While working with EF code first I get error given below at different times: The entity type SomeType is not part of the model for the current context. What are the possible causes of this error? It may occur because: DbContext configured with an incorrect connection string The entity specified is actually not mapped in configuration I got this when my class that inherited from DbContext did not declare the model as a property. For example, I neglected to add a property for FooModel in the code below: public class MyDBContext : DbContext { public DbSet<FooModel> FooModels{ get; set; } // etc.

Guidance for synchronising reverse associations in Entity Framework 4.1

南楼画角 提交于 2019-11-28 09:22:40
EF 4.1 synchronises reverse associations when you create your instances. Is there any documentation of or best practices guidance available for this behaviour? What I mean by synchronising the reverse association is that given: public class Blog { public Blog() { Posts = new List<Blog>(); } public int Id { get; set; } public ICollection<Post> Posts { get; private set; } } public class Post { public Blog Blog { get; set; } public int Id { get; set; } } Then after the following line the Post will have it's Blog property set. var blog = new Blog(); context.Blogs.Add(blog); blog.Posts.Add(new Post

Dynamic Include statements for eager loading in a query - EF 4.3.1

戏子无情 提交于 2019-11-28 09:16:04
I have this method: public CampaignCreative GetCampaignCreativeById(int id) { using (var db = GetContext()) { return db.CampaignCreatives .Include("Placement") .Include("CreativeType") .Include("Campaign") .Include("Campaign.Handshake") .Include("Campaign.Handshake.Agency") .Include("Campaign.Product") .AsNoTracking() .Where(x => x.Id.Equals(id)).FirstOrDefault(); } } I would like to make the list of Includes dynamic. I tried: public CampaignCreative GetCampaignCreativeById(int id, string[] includes) { using (var db = GetContext()) { var query = db.CampaignCreatives; foreach (string include in

Entity Framework Updating Many-To-Many Relationships - POCO

孤街浪徒 提交于 2019-11-28 09:13:13
问题 I have following Many-To-Many relationship between two entities RelayConfig and StandardContact Entities: public class RelayConfig : EntityBase, IDataErrorInfo { ... //Associations public virtual ICollection<StandardContact> StandardContacts { get; set; } } public class StandardContact :EntityBase, IDataErrorInfo { ... //Associations public virtual ICollection<RelayConfig> RelayConfigs { get; set; } } Now I am trying to update RelayConfig and its relations with the StandardContact. Here is

On Insert / Update logic in EF code first

纵饮孤独 提交于 2019-11-28 09:09:10
I would like to add some logic to the insert and update events of some EF objects. I have a MVC application with category object which has a property which is a slugified version of the name property. public class Category { public string Name { get; set; } public string UrlName{ get; set; } } I would like to set the UrlName property only on the insert and update events because my slugify logic is quite elaborate. I am aware that I can add some logic inside the SaveChanges() function on the context itself but I rather would like to put the code closer to the entity itself. Is there a way to

EF 4.1 OnModelCreating not called

余生长醉 提交于 2019-11-28 09:06:27
I have a problem with EF 4.1 not calling OnModelCreating so that I can configure tables etc. I have an existing database. Here is my connection string: <add name="AcmeDBContext" connectionString="metadata=res://*/|res://*/|res://*/; provider=System.Data.SqlClient; provider connection string=" data source=[server]; initial catalog=Acme;integrated security=True; multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> Here is my class inherited from DbContext : public class AcmeDBContext : DbContext { public AcmeDBContext() : base() { Database.SetInitializer

entity framework 4.1 invalid column name

笑着哭i 提交于 2019-11-28 08:34:30
I have two table News and NewsComments. I followed the rules of naming structure NewsComments public class NewsComment : BaseComment { public int NewsId { get; set; } public virtual News News { get; set; } } But query return exception Invalid column name "News_Id". I know what this exception created when in table not related column. CREATE TABLE [dbo].[NewsComments]( [Id] [int] IDENTITY(1,1) NOT NULL, [NewsId] [int] NOT NULL, [Text] [varchar](max) NOT NULL, [UserId] [int] NOT NULL, [CommentDate] [datetime] NOT NULL, [Ip] [varchar](40) NOT NULL, CONSTRAINT [PK_NewsComments] PRIMARY KEY

EF 4.1 and “Collection was modified; enumeration operation may not execute.” exception

守給你的承諾、 提交于 2019-11-28 08:24:09
This has been driving me nuts for the last 2 days. I have 3 pretty basic classes (well, reduced for readability) public class Employee { public string Name { set; get; } virtual public Employer Employer { set; get; } public Employee(string name) { this.Name = name; } } , // this basically ties Employee and his role in a company. public class EmployeeRole{ public int Id { set; get; } virtual public Employee Employee { set; get; } public string Role { set; get; } public EmployeeRole(Employee employee, string role){ this.Employee = employee; this.Role = role; } } and public class Employer{ public