dbcontext

C# EF 4.1 Create table dynamically within DbContext

纵饮孤独 提交于 2019-11-28 04:10:02
问题 I want to add tables to a SQLCe database at runtime, since the tablenames are not static and known at compile time. I try to do this with Entity Framework 4.1 and DbContext as follows: public class PersonContext : DbContext { public PersonContext() : base("UnicornsCEDatabase") { } } public class Person { public int NameId { get; set; } public string Name { get; set; } } public class Program { static void Main(string[] args) { using (var db = new PersonContext()) { db.Database.Delete(); //Try

Is it always better to use 'DbContext' instead of 'ObjectContext'?

时光怂恿深爱的人放手 提交于 2019-11-27 23:52:04
问题 I just downloaded EntityFramework.dll v4.3 . I've found a number of questions that compare DbContext vs. ObjectContext . But most of these are from 2010, or early 2011. I'd like to read more on the subject. Specifically, are there any books on DbContext I can get my hands on? I also want to know, as of today, what are the limitations of DbContext when comparing it to its older brother the ObjectContext ? I realize that DbContext is more compact in that it exposes fewer properties. This

Entity Framework relationships between different DbContext and different schemas

喜你入骨 提交于 2019-11-27 23:36:45
问题 So, I have two main objects, Member and Guild. One Member can own a Guild and one Guild can have multiple Members. I have the Members class in a separate DbContext and separate class library. I plan to reuse this class library in multiple projects and to help differentiate, I set the database schema to be "acc". I have tested this library extensively and can add, delete, and update Members in the acc.Members table. The Guild class is as such: public class Guild { public Guild() { Members =

DbContext won't keep connection open for re-use

随声附和 提交于 2019-11-27 22:11:43
问题 I'm trying to reuse an existing database connection so that I can do multiple database operations using a TransactionScope without invoking MSDTC. Entity Framework (using the new DbContext API in the 4.1 release) doesn't seem to want to keep an explicitly-opened connection open. The old ObjectContext API keeps the connection open as expected and documented. Since the DbContext API just uses ObjectContext under the hood, I'd have expected the same behaviour. Does anyone know if this change is

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

Using DbContext Set<T>() instead of exposing on the context

寵の児 提交于 2019-11-27 21:54:20
Are there any differences when doing the following: public class UsersContext : DbContext { public DbSet<User> Users { get; set; } } versus using the Set<T> method of the context: public class UsersContext : DbContext { } var db = new UsersContext(); var users = db.Set<User>(); These effectively do the same thing, giving me a set of Users, but are there any big differences other than you are not exposing the set through a property? The Users property is added for convenience, so you don't need to remember what all of your tables are and what the corresponding class is for it, you can use

Entity Framework Find vs. Where

不羁岁月 提交于 2019-11-27 20:05:14
Is there a significant difference between .Find(id) and .Where(x = >x.Id == id) that should compel me to use .Find() over .Where()/.First() ? I would imagine that .Find() would be more efficient but is it so much more efficient that I should avoid .Where()/.First() ? The reason I ask is that I am using a generic FakeDbSet in my tests to make it easy to implement fake results and so far I have found that I must inherit that class and provide a custom implementation of .Find() whereas if I write my code with .Where()/.First() I don't need to do that extra work. tschmit007 The point is that find

unexpected GetType() result for entity entry

…衆ロ難τιáo~ 提交于 2019-11-27 19:40:13
While I iterating through ObjectStateEntries I expected [t] variable name will be MY_ENTITY foreach (ObjectStateEntry entry in context.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted)) { Type t = entry.Entity.GetType(); ... } but real I have System.Data.Entity.DynamicProxies.MY_ENTITY_vgfg7s7wyd7c7vgvgv..... How can I determine can I cast current entry to MY_ENTITY type? You can get the original entity type of a proxy type by ObjectContext.GetObjectType(entity.GetType()) This is a static method of ObjectContext , so you can readily use in in a DbContext environment. If for some

One transaction with multiple dbcontexts

守給你的承諾、 提交于 2019-11-27 19:08:12
I am using transactions in my unit tests to roll back changes. The unit test uses a dbcontext, and the service i'm testing uses his own. Both of them are wrapped in one transaction, and one dbcontext is in the block of the other. The thing is, when the inner dbcontext saves his changes, it's not visible to the outer dbcontext (and i don't think it's because the other dbcontext might already have the object loaded). Here is the example: [TestMethod] public void EditDepartmentTest() { using (TransactionScope transaction = new TransactionScope()) { using (MyDbContext db = new MyDbContext()) { /

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