entity-framework-4.1

“Cannot drop database because it is currently in use”. How to fix?

帅比萌擦擦* 提交于 2019-11-26 18:59:00
问题 Having this simple code I get "Cannot drop database "test_db" because it is currently in use" (CleanUp method) as I run it. [TestFixture] public class ClientRepositoryTest { private const string CONNECTION_STRING = "Data Source=.;Initial Catalog=test_db;Trusted_Connection=True"; private DataContext _dataCntx; [SetUp] public void Init() { Database.SetInitializer(new DropCreateDatabaseAlways<DataContext>()); _dataCntx = new DataContext(CONNECTION_STRING); _dataCntx.Database.Initialize(true); }

EF Model First or Code First Approach?

你离开我真会死。 提交于 2019-11-26 18:57:08
问题 I know this question has been asked numerous times before as I’ve read quite a few posts on the topic about the pros and cons etc but I still can’t decide which approach is right for me. I’m very new to web programming and come from a SQL DB Admin / report writing background. I’ve decided to try build my own website which may end up having 30 -40 tables maybe more in the future. I’ve looked at both approaches and I do favour Entity Model approach only because I like simplicity of the designer

StringLength vs MaxLength attributes ASP.NET MVC with Entity Framework EF Code First

随声附和 提交于 2019-11-26 18:54:14
问题 What is the difference in behavior of [MaxLength] and [StringLength] attributes? As far as I can tell (with the exception that [MaxLength] can validate the maximum length of an array) these are identical and somewhat redundant? 回答1: MaxLength is used for the Entity Framework to decide how large to make a string value field when it creates the database. From MSDN: Specifies the maximum length of array or string data allowed in a property. StringLength is a data annotation that will be used for

Overriding SaveChanges and setting ModifiedDate, but how do I set ModifiedBy?

怎甘沉沦 提交于 2019-11-26 18:50:43
I have an ASP.NET MVC3 web application with UI, Business (entities), and Data (DbContext) layers. I am using Entity Framework 4.1 Code First. Right now, I am overriding the DbContext.SaveChanges() in the Data layer so that I can set the ModifiedDate for all changes made to any entity objects that implement my IAuditable interface. I have a static DateProvider class and method (GetCurrentDate) that returns DateTime.Now (unless I'm running a test, in which case, it returns whatever I told it to). I would like to automatically set the ModifiedBy property to the current user as well. What is the

Entity Framework and forced Inner Join

瘦欲@ 提交于 2019-11-26 18:28:27
问题 I have Table1 with the following relationships (they are not enforced they only create the relationship for the navigation properties) Table1 (*)->(1) Table2 Table1 (*)->(1) Table3 Table1 (*)->(1) Table4 Table1 (*)->(1) Table5 Using eager loading code looks like IQueryable<Table1> query = context.Table1s; query = query.Include(Table1 => Table1.Table2); query = query.Include(Table1 => Table1.Table3); query = query.Include(Table1 => Table1.Table4); query = query.Include(Table1 => Table1.Table5)

Entity Framework Code First Many to Many Setup For Existing Tables

假装没事ソ 提交于 2019-11-26 18:26:44
问题 I have the following tables Essence , EssenseSet , and Essense2EssenceSet Essense2EssenceSet is the linking table that creates the M:M relationship. I've been unable to get the M:M relationship working though in EF code first though. Here's my code: [Table("Essence", Schema = "Com")] public class Essence { public int EssenceID { get; set; } public string Name { get; set; } public int EssenceTypeID { get; set; } public string DescLong { get; set; } public string DescShort { get; set; } public

Organizationally, where should I put common queries when using Entity Framework Code First?

三世轮回 提交于 2019-11-26 18:26:15
I'm laying out a new data layer using EF 4.1 Code First, migrating from an older homebrew data layer. I have set up two assemblies, one for my context and one for all the POCO code first classes. I have some business logic, for instance, a query against one table (or a few tables) that is used in several different places. Where should I put it? It can't go in a POCO class because it joins a couple tables and so needs a context. It could go in the context, but that context would become bloated with hundreds of disorganized queries. Is there a common pattern or arrangement for all the business

How to pass parameters to the DbContext.Database.ExecuteSqlCommand method?

纵饮孤独 提交于 2019-11-26 18:20:55
Let's just suppose I have a valid need for directly executing a sql command in Entity Framework. I am having trouble figuring out how to use parameters in my sql statement. The following example (not my real example) doesn't work. var firstName = "John"; var id = 12; var sql = @"Update [User] SET FirstName = @FirstName WHERE Id = @Id"; ctx.Database.ExecuteSqlCommand(sql, firstName, id); The ExecuteSqlCommand method doesn't allow you to pass in named parameters like in ADO.Net and the documentation for this method doesn't give any examples on how to execute a parameterized query. How do I

EF Code First: How to get random rows

淺唱寂寞╮ 提交于 2019-11-26 17:39:14
How can I build a query where I would retrieve random rows? If I were to write it in SQL then I would put an order by on newid() and chop off n number of rows from the top. Anyway to do this in EF code first? I have tried creating a query that uses newid() and executing it using DbSet.SqlQuery(). while it works, its not the cleanest of solutions. Also, tried retrieve all the rows and sorting them by a new guid. Although the number of rows are fairly small, its still not a good solution. Any ideas? SLaks Just call: something.OrderBy(r => Guid.NewGuid()).Take(5) Comparing two options: Skip

Better way to query a page of data and get total count in entity framework 4.1?

偶尔善良 提交于 2019-11-26 17:19:32
Currently when I need to run a query that will be used w/ paging I do it something like this: //Setup query (Typically much more complex) var q = ctx.People.Where(p=>p.Name.StartsWith("A")); //Get total result count prior to sorting int total = q.Count(); //Apply sort to query q = q.OrderBy(p => p.Name); q.Select(p => new PersonResult { Name = p.Name }.Skip(skipRows).Take(pageSize).ToArray(); This works, but I wondered if it is possible to improve this to be more efficient while still using linq? I couldn't think of a way to combine the count w/ the data retrieval in a single trip to the DB w