entity-framework-4.1

Atomic Increment with Entity Framework

天大地大妈咪最大 提交于 2019-11-26 21:13:21
问题 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

EF Code First - how to set identity seed?

爷,独闯天下 提交于 2019-11-26 20:52:44
I have a entity class public class Employee { public long Id { get; set; } public string Name { get; set; } } I have set the Id field as the primary key with auto number generation modelBuilder.Entity<Employee>().HasKey(e => e.Id); modelBuilder.Entity<Employee>().Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); But I want the Identity to seed from 10000 instead of from 1 which is the default. How can I specify this in EF? Ladislav Mrnka If you are using SQL Server you must create custom database initializer and manually execute DBCC CHECKIDENT ('TableName',

How to configure many to many relationship using entity framework fluent API

好久不见. 提交于 2019-11-26 20:45:48
I'm trying to set up a many to many relationship in EF code first but the default conventions is getting it wrong. The following classes describes the relationship: class Product { public int Id { get; set; } public string Name { get; set; } } class Account { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Product> Products { get; set; } } One Account can have many Products. However the EF conventions will create the DB tables as: Products Table -------------- Id Name Account_Id <- What is this? Accounts Table -------------- Id Name This doesn't look

Can you get the DbContext from a DbSet?

安稳与你 提交于 2019-11-26 20:34:38
问题 In my application it is sometimes necessary to save 10,000 or more rows to the database in one operation. I've found that simply iterating and adding each item one at a time can take upwards of half an hour. However, if I disable AutoDetectChangesEnabled it takes ~ 5 seconds (which is exactly what I want) I'm trying to make an extension method called "AddRange" to DbSet which will disable AutoDetectChangesEnabled and then re-enable it upon completion. public static void AddRange<TEntity>(this

Is UnitOfWork and GenericRepository Pattern redundant In EF 4.1 code first?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 20:04:25
Wondering if I need to use the Genericrepository pattern and UnitOfWork to mock the repository.I am using MOQ.Is it now redundant since I have noticed that EF 4.1 has IDBSet. I have not figured out how to write something generic that usic IDBSet .If you have an example where you implement IDBSet can you show it to me? Any suggestions? Ladislav Mrnka This is duplicate of many topics already discussed on SO but I agree that some of them can be hard to find because they are nested in other question What's the point of Generic repository in EF 4.1 Challenges with testable and mockable code in EF

EF 5 Changing Connection String at Runtime

只愿长相守 提交于 2019-11-26 19:57:47
问题 Ok, I want to recreate a project that I created using EF 4.1 to EF 5.0, simple enough or at least I thought. One of the things in my old project is that I was able to change the database connection string at runtime in EF 4.1: using (var myContext = new MyEntities(ConnectionString)) { } Easy-peasy but in EF 5.0 you have to do this differently: string connectionString = "data source=LocalHost;initial catalog=MyDatabase;user id=MyUserName;password=MyPassword;multipleactiveresultsets=True;App

How to include a child object's child object in Entity Framework 5

北城余情 提交于 2019-11-26 19:44:14
I am using Entity Framework 5 code first and ASP.NET MVC 3 . I am struggling to get a child object's child object to populate. Below are my classes.. Application class; public class Application { // Partial list of properties public virtual ICollection<Child> Children { get; set; } } Child class: public class Child { // Partial list of properties public int ChildRelationshipTypeId { get; set; } public virtual ChildRelationshipType ChildRelationshipType { get; set; } } ChildRelationshipType class: public class ChildRelationshipType { public int Id { get; set; } public string Name { get; set; }

Primary /Foreign Key in Entity Framework

只愿长相守 提交于 2019-11-26 19:35:33
问题 I have created an entity class in my MVC 3 application. One of the attribute named RegistryId is primary key as well as foreign key. How can I make a column primary key as well as Foreign key ? I am not using EF ORM designer. I am coding classes by hand. 回答1: I think by "not using EF ORM designer" you mean new DbContext API from EF 4.1. Because if you don't mean DbContext API you still have to use EDMX (designer). You can either use data annotations ( System.ComponentModel.DataAnnotations ):

Get Auto Identity Key after Insert via EF

回眸只為那壹抹淺笑 提交于 2019-11-26 19:33:57
问题 Is there a straight forward way of retrieving a DB auto generated primary key when adding a record via Entity Framework 4.1? For example: dbcontext.Entity_Tables.Add(new Entity_Table { item1 = val1, item2 = val2 }); dbcontext.SaveChanges(); newPK = ???; The SQL equivalent would be: newPK = executeOnDB("INSERT INTO Entity_Table (item1, item2) VALUES (val1, val2);SELECT @@Indentity";); BTW I'm using MySQL but the SQL would be the same as on MSSQL 回答1: I believe EF should update your entity

What is the best way to instantiate and dispose DbContext in MVC?

北城以北 提交于 2019-11-26 19:23:24
问题 MVC 3 + EF 4.1 I'm choosing between two approaches to deal with DbContext: Instantiate in Application_BeginRequest , put it into HttpContext.Current.Items and dispose in Application_EndRequest . Create disposable UnitOfWork (kindof wrapper for DbContext ) and start each controller action with using(var unitOfWork = new UnitOfWork()) { ... } Share your experience please: Which one would you prefer? what are pros and cons for each approach? 回答1: I would suggest you use a Dependency Injection