entity-framework-6

Entity Framework 6 Code First Custom Functions

六月ゝ 毕业季﹏ 提交于 2019-12-17 05:03:13
问题 I'm trying something similar to this: How to use scalar-valued function with linq to entity? However I'm not using EDMX, but instead just DbContext and code first. I've come across this: https://codefirstfunctions.codeplex.com/ But the usage isn't suitable. What I am trying to achieve is to be able to do this: var locations = context.Locations.Where(e => Functions.LatLongDistanceCalc(e.Lat, e.Long, lat, long) >= 10) Where it will call a scalar function (LatLongDistanceCalc) on SQL Server. Is

Mapping composite keys using EF code first

我只是一个虾纸丫 提交于 2019-12-17 03:54:57
问题 Sql server table: SomeId PK varchar(50) not null OtherId PK int not null How should I map this in EF 6 code first? public class MyTable { [Key] public string SomeId { get; set; } [Key] public int OtherId { get; set; } } I've seen some examples where you have to set the order for each column, is that required? Is there official documentation on this somewhere? 回答1: You definitely need to put in the column order, otherwise how is SQL Server supposed to know which one goes first? Here's what you

Mapping composite keys using EF code first

亡梦爱人 提交于 2019-12-17 03:54:15
问题 Sql server table: SomeId PK varchar(50) not null OtherId PK int not null How should I map this in EF 6 code first? public class MyTable { [Key] public string SomeId { get; set; } [Key] public int OtherId { get; set; } } I've seen some examples where you have to set the order for each column, is that required? Is there official documentation on this somewhere? 回答1: You definitely need to put in the column order, otherwise how is SQL Server supposed to know which one goes first? Here's what you

Entity Framework 6 transaction rollback

﹥>﹥吖頭↗ 提交于 2019-12-17 03:53:46
问题 With EF6 you have a new transaction which can be used like: using (var context = new PostEntityContainer()) { using (var dbcxtransaction = context.Database.BeginTransaction()) { try { PostInformation NewPost = new PostInformation() { PostId = 101, Content = "This is my first Post related to Entity Model", Title = "Transaction in EF 6 beta" }; context.Post_Details.Add(NewPost); context.SaveChanges(); PostAdditionalInformation PostInformation = new PostAdditionalInformation() { PostId = (101),

Entity Framework Insert bug?

烈酒焚心 提交于 2019-12-14 04:07:20
问题 I used EF 6.2.0 DB first, SQL Server 2017 I created a table to test this create table Person ( ID int primary key, Name varchar(50) ) And I created a form to insert, this is button click event: private void btnSubmit_Click(object sender, RoutedEventArgs e) { Person p = new Person(); p.id = Convert.ToInt32(txbID.Text); p.name = txbName.Text; try { db.People.Add(p); db.SaveChanges(); } catch (Exception ex) { Console.WriteLine("###Ex:" + ex.ToString()); MessageBox.Show(ex.ToString()); } } First,

How to get connection string in class library project

不羁的心 提交于 2019-12-14 03:48:36
问题 In my .net solution, I have two different projects: An MVC core web application project and a class library project. In web application project, database connection string is in appsettings.json file. I would like to access that connection string from class library project. Is it possible? If yes, how? If there is any better approach to get connection string, please let me know. 回答1: To make simple and trivial example you could create a class in your class library that will represent

Is there a simple way using data annotations or a custom type to use a value stored as a string in SQL as a DateTime in EF?

假装没事ソ 提交于 2019-12-14 03:26:13
问题 I have a database where all of the dates and times are stored as strings [yyyyMMdd] and [hhmmss] respectively. Is there a data annotation I can add in the POCO class so that it is recognised as the type that it should be? EG: [Column(TypeName="varchar(8)", Format="yyyyMMdd"] // Format does not exist! public DateTime MyDate { get; set; } Or if not, is there a way to define a custom Type and specify how EF should convert the data between the property value and SQL? NOTE: I have no desire to use

Entity Framework Lambda predicate stored in var with association

孤者浪人 提交于 2019-12-14 03:22:25
问题 I have a generic repository using EF6. The issue has to do with association properties requiring an "Include" even though it shouldn't. The following works: IQueryable<User> dbQuery = _db.Set<User>(); return dbQuery.Where(x => x.Child.Name == "Foo").ToList(); However, the following does not work: Func<User, bool> filter = x => x.Child.Name == "Foo"; IQueryable<User> dbQuery = _db.Set<User>(); return dbQuery.Where(filter).ToList(); It throws an "Object Reference not set..." exception on Child.

Entity Framework The context cannot be used while the model is being created

牧云@^-^@ 提交于 2019-12-14 02:02:55
问题 My unit of work class is mentioned below and I am using Ninject and I have tried injecting IUnitOfWork per request per thread scope, transient etc. but I am still getting error which is: "Message":"An error has occurred.","ExceptionMessage":"The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of

Entity Framework Error Deleting Entity with Foreign Key Relationship

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-14 01:47:21
问题 I am having a problem deleting some entities due to a foreign key relationship. I understand the following error message and have been doing everything I can think of to delete the entities without incurring this error: The DELETE statement conflicted with the REFERENCE constraint "FK_QuizUserAnswer_QuizWithQuestion". The conflict occurred in database "SomeDatabase", table "dbo.QuizUserAnswer", column 'idQuizQuestion'. The statement has been terminated. Here is an image of the two tables in