entity-framework

Entity Framework DbContext constructor with connection string

时光总嘲笑我的痴心妄想 提交于 2020-01-10 19:42:09
问题 Can someone help me understand the following two approaches to pass a connection string to the DbContext ? Approach #1: public EWebDBContextEMS() : base("mainConnectionString") { } and approach #2: public EWebDBContextEMS() : base("name=mainConnectionString") { } This article states that name=... will get created by designer, but I tested with pure DbContext code, it works as well. Is this an intented behaviour of the DbContext constructor? And in the documentation, it does not mention that

Entity Framework DbContext constructor with connection string

笑着哭i 提交于 2020-01-10 19:42:09
问题 Can someone help me understand the following two approaches to pass a connection string to the DbContext ? Approach #1: public EWebDBContextEMS() : base("mainConnectionString") { } and approach #2: public EWebDBContextEMS() : base("name=mainConnectionString") { } This article states that name=... will get created by designer, but I tested with pure DbContext code, it works as well. Is this an intented behaviour of the DbContext constructor? And in the documentation, it does not mention that

How do I tell Entity Framework Function Import that a column returned by a stored procedure is not nullable?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-10 17:39:27
问题 I have an SQL Server stored procedure that ressembles this: CREATE PROCEDURE [jp].[GetFoo] @Guid UNIQUEIDENTIFIER AS SELECT CONVERT(BIT, (CASE WHEN [dbo].[GetBar](T.Col2) = 3 THEN 1 ELSE 0 END)) IsGetBarCol2EqualToThree FROM [dbo].[MyTable] T WHERE T.Col1 = @Guid When I do Function Import / Get Column Information in EF, the inferred type of the column IsGetBarCol2EqualToThree is Nullable<bool> . But there is no way this field is going to be null, so I'd like it to be just bool . Is there a

AsNoTracking() and Include

纵然是瞬间 提交于 2020-01-10 17:26:27
问题 I have a Linq query that fetches an entity and some of its navigation properties. context.MyEntity .AsNoTracking() .Include(i=> i.Nav1) .Include(i=> i.Nav2) .Where(x=> x.Prop1==1) .FirstOrDefault(); my question is: Is the above query enough to not track MyEntity nor the navigation properties NAv1 & Nav2 or must I add AsNoTracking for each navigation property? like this: context.MyEntity .AsNoTracking() .Include(i=> i.Nav1) .AsNoTracking() .Include(i=> i.Nav2) .AsNoTracking() .Where(x=> x

entity framework entity sql vs linq to entities

谁都会走 提交于 2020-01-10 14:32:06
问题 what's the purpose of entity sql, i mean if you have linq to entities why would you need to write queries in string, are there any performance reasons or something ? 回答1: LINQ to Entities does not allow you access to every feature of your database. Being able to "reach into" the database is sometimes necessary for advanced queries, either to pull them off in the first place or to improve the sometimes horrible choices that the LINQ to Entities system will make about your query. That said, I

Manage the lifetime of dbContext

a 夏天 提交于 2020-01-10 14:08:17
问题 I would like to tie the lifetime of a dbContext to the lifetime of a session, to - for example - be able to commit or abandon changes on a group of mutations on the dbcontext over multiple requests. Are there any other (better?) ways to accomplish this? If no, what would be a suitable mechanism to create and dispose of the contexts? I am thinking about static hashtables with cleanup on session end, but maybe I'm doing it All Wrong. I am also thinking about the idea of only holding on to those

Save files in database with entity framework

强颜欢笑 提交于 2020-01-10 09:56:08
问题 I have an ASP.NET MVC solution built on Entity Framework with Microsoft SQL Server 2008. I need to create a function that lets my users upload files. What I would like is: A solution that uses the Entity Framework to store files in the Database A solution that detects and prevents from uploading the same file twice via some kind of hash/checksum Tips on database/table design 回答1: The "right" way to store a file in a SQL Server 2008 database is to use the FILESTREAM data type. I'm not aware

Save files in database with entity framework

天涯浪子 提交于 2020-01-10 09:56:06
问题 I have an ASP.NET MVC solution built on Entity Framework with Microsoft SQL Server 2008. I need to create a function that lets my users upload files. What I would like is: A solution that uses the Entity Framework to store files in the Database A solution that detects and prevents from uploading the same file twice via some kind of hash/checksum Tips on database/table design 回答1: The "right" way to store a file in a SQL Server 2008 database is to use the FILESTREAM data type. I'm not aware

Code First vs. Database First

一个人想着一个人 提交于 2020-01-10 09:38:11
问题 I created an Entity Framework model based on an existing database, then generated the POCO entities from the model. The connection string in my web.config isn't Entity Framework, it's just the standard connection string (it's missing the CSDL, SSDL, MSL references). I can compile my application, but when I run I get this error: Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database

How to ignore a DbUpdateConcurrencyException when deleting an entity

*爱你&永不变心* 提交于 2020-01-10 08:54:09
问题 I have an app that reads a lot of data into memory and processes it in a batches. What I want is for entity framework to ignore DbUpdateConcurrencyException when deleting an entity that has already been deleted. The reason is that by the time an entity has been processed and marked for deletion, it may already have been deleted from the DB. Obliviously deleting a row that has already been deleted isn't a problem and shouldn't cause an error, I just need a way to tell entity framework that :)