entity-framework-4.1

Can you get the DbContext from a DbSet?

走远了吗. 提交于 2019-11-27 22:00:23
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 DbSet<TEntity> set, DbContext con, IEnumerable<TEntity> items) where TEntity : class { // Disable auto

EF 4.1 CF: CREATE DATABASE permission denied in database 'master'

自作多情 提交于 2019-11-27 21:51:33
问题 Entity Framework 4.1 Code First works great with SQLEXPRESS on localhost . However, I'm now ready to connect to a regular SQL 2008 server . I created a new database "NewEfDatabase". Then changed my " ApplicationServices " connectionString in Web.config to point to my new database with integrated security. But then I get this error: " CREATE DATABASE permission denied in database 'master'. " So... a) What permissions does EF 4.1 CF need on said SQL server to do its work? b) Can I setup an

Command Timeout with Entity Framework 4.1 Code First

强颜欢笑 提交于 2019-11-27 21:35:48
问题 How can I set the command timeout of a DbContext? 回答1: I found this solution after another Google search. You can access the ObjectContext for a DbContext by casting this to an IObjectContextAdapter. From http://social.msdn.microsoft.com/Forums/en-ZA/adodotnetentityframework/thread/6fe91a64-0208-4ab8-8667-d061af340994: public class MyContext : DbContext { public MyContext () : base(ContextHelper.CreateConnection("my connection string"), true) { ((IObjectContextAdapter)this).ObjectContext

Entity Framework - How to check if table exists?

China☆狼群 提交于 2019-11-27 21:11:18
I'm using the Entity Framework with Code First approach. The base class DbContext has functions to create and delete the database as well as to check for its existence. I want to check if a special table (entity) is existing or not. Is it possible with an framework implementation or do I need to write custom methods? If I need to write my own implementation, what would be the most generic approach to do that? Thanks for any help. If you need to check existence of the table you must call custom SQL code: bool exists = context.Database .SqlQuery<int?>(@" SELECT 1 FROM sys.tables AS T INNER JOIN

Entity Framework/MVC3: temporarily disable validation

帅比萌擦擦* 提交于 2019-11-27 20:16:58
问题 I am using EF Code First. I heavily use code annotations to specify how the data (model) should be validated. However, often I just need validation at UI layer and be able to suppress validation when I save certain data to the database in the code. However, once I specified validation rules, they are applied everywhere -- on the UI, on the database, on the data access layer. Can I temporarily disable model validation at EF layer so I can save the data using SaveChanges() without getting

EF Code First Additional column in join table for ordering purposes

青春壹個敷衍的年華 提交于 2019-11-27 19:51:17
I have two entities that have a relationship for which I create a join table public class Student { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Image> Images { get; set; } } public class Image { public int Id { get; set; } public string Filename { get; set; } public virtual ICollection<Student> Students { get; set; } } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Student>() .HasMany(i => i.Images) .WithMany(s => s.Students) .Map(m => m.ToTable("StudentImages")); } I would like to add an additional column

Setting a foreign key to null when using entity framework code first

萝らか妹 提交于 2019-11-27 19:18:51
I'm using the database first implementation of Entity Framework Code First as the data layer for a project, but I've run into a problem. I need to be able to set a foreign key to null in order to remove an association in the database. I have 2 objects. One is called Project. public class Project { public int ProjectId {get; set;} public Employee Employee {get;set;} } public class Employee { public int EmployeeId {get; set;} public string EmployeeName {get;set;} } This matches what I have in the Database: CREATE TABLE Project( ProjectId int IDENTITY(1,1) NOT NULL, EmployeeId int NULL ) CREATE

Primary /Foreign Key in Entity Framework

让人想犯罪 __ 提交于 2019-11-27 18:45:39
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. 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 ): KeyAttribute and ForeignKeyAttribute : public class Registry { public virtual int Id { get; set; } public

Get Auto Identity Key after Insert via EF

爱⌒轻易说出口 提交于 2019-11-27 18:42:29
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 I believe EF should update your entity object with the identity: var entity = new Entity_Table { item1 = val1, item2 = val2 }; dbcontext.Entity_Tables

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

南笙酒味 提交于 2019-11-27 18:09: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? I would suggest you use a Dependency Injection framework. You can register your DbContext as per request container.RegisterType<MyDbContext>()