entity-framework-4.1

Why does the Entity Framework's DbContext.Find() generate a query with select top 2?

╄→尐↘猪︶ㄣ 提交于 2019-12-18 19:45:29
问题 Why does the Entity Framework's DbContext.Find() generate a query with select top 2 and a derived table? By definition, the query is looking up by primary key which should be unique. 回答1: Find checks first if the entity with the given key is already in the context. If not it queries the database. Possibly it uses in this case a LINQ query using SingleOrDefault . SingleOrDefault translates to SELECT TOP 2 to be able to throw an exception if the result has more than one entity. So, why doesn't

EF 4.1 - Model Relationships

痞子三分冷 提交于 2019-12-18 18:48:11
问题 I'm trying to create a quick ASP.NET MVC 3 application using the RC version of EF 4.1. I have two models: public class Race { public int RaceId { get; set; } public string RaceName { get; set; } public string RaceDescription { get; set; } public DateTime? RaceDate { get; set; } public decimal? Budget { get; set; } public Guid? UserId { get; set; } public int? AddressId { get; set; } public virtual Address Address { get; set; } } and public class Address { public int AddressId { get; set; }

Simpleinjector: Is this the right way to RegisterManyForOpenGeneric when I have 2 implementations and want to pick one?

爱⌒轻易说出口 提交于 2019-12-18 17:26:16
问题 Using simple injector with the command pattern described here and the query pattern described here. For one of the commands, I have 2 handler implementations. The first is a "normal" implementation that executes synchronously: public class SendEmailMessageHandler : IHandleCommands<SendEmailMessageCommand> { public SendEmailMessageHandler(IProcessQueries queryProcessor , ISendMail mailSender , ICommandEntities entities , IUnitOfWork unitOfWork , ILogExceptions exceptionLogger) { // save

EF Code First: Many-to-many and one-to-many

主宰稳场 提交于 2019-12-18 16:48:48
问题 This is probably just because my knowledge with the EF Code First fluent API is lacking, but I'm stumped. I want to model the following: A Groups collection with Id and Name A Users collection with Id and Name Each user is assigned to exactly one primary group Each user may have zero or many secondary groups The table structure I'm going for would look like: Groups Id Name Users Id Name PrimaryGroupId SecondaryGroupAssignments UserId GroupId I've been beating my head against a wall trying to

Pros and Cons of putting a db context in static class library

◇◆丶佛笑我妖孽 提交于 2019-12-18 15:54:47
问题 We have a static class library to deal with repeated multi-contextual tasks. Is it bad practice to create an EF db context as member of a static class? DB contexts are meant to be disposed of for a reason. Having them disposed frequently keeps the connection pool "flowing" and probably (here I'm speculating) assures that tables don't remain locked. So am I inviting trouble having a db context in a static class or am I overthinking this? 回答1: IMO this is definitally not something you want to

How do I upsert a record in ADO.NET EF 4.1?

久未见 提交于 2019-12-18 14:52:18
问题 I'm trying to accomplish something really simple and I can't find how to do it using Entity Framework 4.1. I want a controller method that accepts an object and then does an UPSERT (either an insert or update depending on whether the record exists in the database). I am using a natural key, so there's no way for me to look at my POCO and tell if it's new or not. This is how I am doing it, and it seems wrong to me: [HttpPost] public JsonResult SaveMyEntity(MyEntity entity) { MyContainer db =

How do I upsert a record in ADO.NET EF 4.1?

▼魔方 西西 提交于 2019-12-18 14:51:53
问题 I'm trying to accomplish something really simple and I can't find how to do it using Entity Framework 4.1. I want a controller method that accepts an object and then does an UPSERT (either an insert or update depending on whether the record exists in the database). I am using a natural key, so there's no way for me to look at my POCO and tell if it's new or not. This is how I am doing it, and it seems wrong to me: [HttpPost] public JsonResult SaveMyEntity(MyEntity entity) { MyContainer db =

How do I allow the EF4 CodeFirst database initializer to run under development, but not run in production

谁说我不能喝 提交于 2019-12-18 13:38:03
问题 I am attempting to deploy my first alpha version of a system online for a few people to start using. On development I make heavy use of the DropCreateDatabaseOnModelChange<TContext> (I don't have it in front of me at the moment so I can't verify the exact name) to re-initialize my dev database every time my model changes. This happens in the Global.asax . However, I do not want this to happen on my web host where other people are entering real data. I need to handle all db migrations on there

Using EF4 Code First: How can I change the Model without losing data

我只是一个虾纸丫 提交于 2019-12-18 12:29:49
问题 In my Global.asax I have the following line: Database.SetInitializer<myDbSupport> (new DropCreateDatabaseIfModelChanges<myDbSupport>()); If I don't have this, then when i change the model, the sdf database will not have the correct structure. This is ok in a dev environment, but when I move to production and want a DB structure update, i of course, can't afford to drop the table, and lose the data. Is it possible to script DB changes, and run this update before deploying the model with the

EF 4.1 code-first adding a trigger to a table

一笑奈何 提交于 2019-12-18 12:21:57
问题 What is the best way to add a trigger to a table created by code-first approach with EF 4.1? A way I'm cosidering is to execute a custom SQL query either in OnModelCreating or when Db context is being initialized. Is there a better idea? 回答1: Using custom initializer which will execute CREATE TRIGGER SQL command is the only option if you want EF to create trigger for you (similar code like here). Also don't forget to include SET NOCOUNT ON at the beginning of the trigger code. But there is