entity-framework-5

EF 5.0 Model First - How To Make Not Mapped Properties

谁说我不能喝 提交于 2019-12-10 10:17:17
问题 Code First has the attribute [NotMapped] for properties which are not to be mapped: how would one achieve the same thing in model-first mode? 回答1: NotMapped properties are not part of your mapping. When the model first approach is used EDMX represents the mapping so every property defined in the diagram is mapped. If you want to have non-mapped property it must not be defined in the diagram. All classes generated by EF's code generation are partial so you only need to create your own partial

Inserting current Database date time via Entity framework

十年热恋 提交于 2019-12-10 09:56:19
问题 We want to put the current datetime into a database column. We have a web farm where the time on the web servers can vary, we therefore need to use the datetime on the database server. We have a database with a Not Null datetime column. This column has a default datetime of current time. This works fine when we insert data using a SQL statement that does not include the datetime column. From Entity Framework: If we define the datetime as not null , the datetime is set to low date, and low

Entity Framework DB First: Timestamp column not working

放肆的年华 提交于 2019-12-10 05:27:51
问题 Using db first approach, I want my application to throw a concurrency exception whenever I try to update an (out-of-date) entity which it's correspoinding row in the database has been already updated by another application/user/session. I am using Entity Framework 5 on .Net 4.5. The corresponding table has a Timestamp column to maintain row version. 回答1: I have done this in the past by adding a timestamp field to the table you wish to perform a concurrency check. (in my example i added a

Entity Framework model change error

你离开我真会死。 提交于 2019-12-10 02:50:24
问题 I am getting the error "The model backing the 'DataContext' context has changed since the database was created. Consider using Code First Migrations to update the database". I am using Entity Framework and have changed my model after declaring DataContext . How can I fix this error? 回答1: If you already deployed your application or you don't want remove data from database you must read about Code First Migrations. Here you have a link: http://msdn.microsoft.com/en-us/library/hh770484(v=vs.103)

How to enable concurrency checking using EF 5.0 Code First?

给你一囗甜甜゛ 提交于 2019-12-10 02:48:58
问题 I would like to do a check-then-update in an atomic operation. I am using dbcontext to manage the transaction. I was expecting to get an exception if the record has been modified by another thread but no exception is thrown. Any help would be appreciated. Here's my output: Thread-4: Reading... Thread-5: Reading... Thread-5: Updating destination 1 Thread-4: Updating destination 1 Thread-4: SaveChanges Thread-5: SaveChanges Here's my code snippet: public static void Main(string[] args) {

Entity Framework 6 Error Unable to load the specified metadata resource

↘锁芯ラ 提交于 2019-12-10 01:21:41
问题 I'm using Entity Framework 6 and "model first" in my solution, I separated my "Data Model" classes into another project, so that I can add reference to the "Data Model" classes without exposing my "Data Model Contexts" and connections. I don't want to expose my Entity Data Model project (especially the DB Context etc) to my UI Layer. I have this: I have now successfully separated my auto generated entity classes from my data model, I tried it this works by adding an entity or a property to an

When to call SaveChanges

馋奶兔 提交于 2019-12-09 17:56:10
问题 Say I have something like this that is called in Controller : using (var context = new SqlContext()) { context.Items.Add(new Item("item1")); } Should I be calling context.SaveChanges(); ? 回答1: entity framework implements a unit of work pattern with DbContext this means that you define a package of things you want to do to your database and then call save changes to propogate them all to the database at once. All operations will be executed within a single transaction (for a single saveChanges

Entity framework manually deleted tables cant be generated from EF migration

谁说胖子不能爱 提交于 2019-12-09 16:06:17
问题 I have created migration and created the database and the tables . For example the tables are A B C D E . Now again I have changed some part of code and ran update-database command . Everything went smooth and nice and the tables showed the columns . Now accidentally I manually deleted one two tables D and E . Now when I try to run the migration with update-database . It runs properly but doesn't create the tables which I deleted manually . I tried to delete the existing migration and re-run

Entity Framework AsNoTracking breaks call to Distinct

╄→尐↘猪︶ㄣ 提交于 2019-12-09 16:02:45
问题 I am trying to load a list of distinct colors from previously loaded list of products on a page. So to pull in the products I do this: var products = Products .Include(p => p.ProductColor) .ToList(); Then I do some processing on the products them I want to get a list of all of the distinct colors used by the products, so I do this: var colors = products .Select(p => p.ProductColor) .Distinct(); And this works great, however if I add a call to .AsNoTracking() to the original products call, I

Best way to concat strings and numbers in SQL server using Entity Framework 5?

我是研究僧i 提交于 2019-12-09 13:18:54
问题 For some reason Microsoft decided to not support simple concat in EF5. e.g. Select(foo => new { someProp = "hello" + foo.id + "/" + foo.bar } This will throw if foo.id or foo.bar are numbers. The workaround I've found is apparently this pretty peice of code: Select(foo => new { someProp = "hello" + SqlFunctions.StringConvert((double?)foo.id).Trim() + "/" + SqlFunctions.StringConvert((double?)foo.bar).Trim() } Which works fine, but is just horrid to look at. So, is there some decent way to