entity-framework

How can I configure Entity Framework to automatically trim values?

早过忘川 提交于 2020-06-22 04:26:29
问题 I need to make “EF Core 2.1.0” remove white space from Strings fields in queries, “HasConversion” not is not working, can you tell me why? entity.Property(e => e.Name) .HasConversion( new ValueConverter<string, string>(v => v.TrimEnd(), v => v.TrimEnd())); -using DB2 database and .net core 2.1 Query: public List<ItemServico> List() { return _uow._db.ItensServico.ToList(); } 回答1: That's what the varchar type is for, to trim spaces automatically, and efficiently. Manual trim() operations have

How can I configure Entity Framework to automatically trim values?

自古美人都是妖i 提交于 2020-06-22 04:25:08
问题 I need to make “EF Core 2.1.0” remove white space from Strings fields in queries, “HasConversion” not is not working, can you tell me why? entity.Property(e => e.Name) .HasConversion( new ValueConverter<string, string>(v => v.TrimEnd(), v => v.TrimEnd())); -using DB2 database and .net core 2.1 Query: public List<ItemServico> List() { return _uow._db.ItensServico.ToList(); } 回答1: That's what the varchar type is for, to trim spaces automatically, and efficiently. Manual trim() operations have

No connection string named 'ConnectionString' could be found in the application config file (EF parameterless constructor )

∥☆過路亽.° 提交于 2020-06-17 00:50:53
问题 When I create a new Dev Express XAF application using the wizard 20.1.3 for .netcore3.1 the code works fine. I can enable migrations and run a migration without problems. (Or so I thought ... see below) However for certain reasons (my legacy call run-migrations code) I want to provide the connection string location to the constructor When I do this, and try to add a migratiion I get an error The DbContext is set up as using System; using System.Data.Entity; using System.Data.Common; using

No connection string named 'ConnectionString' could be found in the application config file (EF parameterless constructor )

天涯浪子 提交于 2020-06-17 00:49:31
问题 When I create a new Dev Express XAF application using the wizard 20.1.3 for .netcore3.1 the code works fine. I can enable migrations and run a migration without problems. (Or so I thought ... see below) However for certain reasons (my legacy call run-migrations code) I want to provide the connection string location to the constructor When I do this, and try to add a migratiion I get an error The DbContext is set up as using System; using System.Data.Entity; using System.Data.Common; using

EF Code first NotMapped Attribute

柔情痞子 提交于 2020-06-16 08:01:39
问题 Why is in the following example the [NotMapped] attribute required: public virtual ICollection<Blog> Blogs { get; set; } [NotMapped] public List<Blog> NewBlogs{ get{ return Blogs.Where(x=>x.Date > DateTime.Now).ToList(); } } Without the [NotMapped] attribute I get an exception: Invalid column name Blog_ID The column name in the database is BlogID. EDIT I would expect, that properties without setter are never directly mapped to the database and automatically ignored by code first. 回答1: with

EF - “Update-Database” causing Sequence contains more than one element

你离开我真会死。 提交于 2020-06-14 06:47:11
问题 I have just finished modifying my models, ran "Update-Database" in the package manager console and BOOM! I received a "Sequence contains more than one element" error. Upon scanning through the console, it did say No pending explicit migrations. which is obviously wrong since I renamed some models. I also found a SingleOrDefault call I don't know where it came from. I commented out my seed method so that's not causing it No pending explicit migrations. System.InvalidOperationException:

EF - “Update-Database” causing Sequence contains more than one element

陌路散爱 提交于 2020-06-14 06:47:08
问题 I have just finished modifying my models, ran "Update-Database" in the package manager console and BOOM! I received a "Sequence contains more than one element" error. Upon scanning through the console, it did say No pending explicit migrations. which is obviously wrong since I renamed some models. I also found a SingleOrDefault call I don't know where it came from. I commented out my seed method so that's not causing it No pending explicit migrations. System.InvalidOperationException:

Should I call SaveChanges once or after each change?

血红的双手。 提交于 2020-06-11 22:45:32
问题 I need to make several changes in my database in my controller. foreach (var valueStream in model.ListValueStream) { ValueStreamProduct vsp = new ValueStreamProduct(valueStream.Id, product.Id); db.ValueStreamProduct.Add(vsp); } db.SaveChanges(); Shoul I call SaveChanges at the end or each time I make changes? 回答1: It depends. With each change - If you want each save to run in its own transaction and be independent of other changes then run the save in the loop or after you make a change. Note

Get List of Modified Objects within Entity Framework 7

こ雲淡風輕ζ 提交于 2020-06-11 20:10:21
问题 I am stumped - upgrading to Entity Framework 7 and I typically override the SaveChanges inside the DbContext to be able to get a list of all the Modified Objects before it changes. Ultimately I have a script that fires that tracks the previous version in a database. In Entity Framework 6 I would get the model changes like so: var oc = ((IObjectContextAdapter)this).ObjectContext; var modifiedItems = oc.ObjectStateManager.GetObjectStateEntries(EntityState.Modified | EntityState.Deleted); List

How to execute a full text search using entity framework 6

邮差的信 提交于 2020-06-10 07:32:07
问题 I have the query: var query = DataContext.Fotos.Where(x => x.Pesquisa.Contais("myTerm") The SQL generated is: SELECT ... FROM Fotos AS [Extent1] WHERE [Extent1].[Pesquisa] LIKE N'%mytem%' But I need to use: SELECT ... FROM Fotos AS [Extent1] WHERE CONTAINS ([Extent1].[Pesquisa], 'my term') How to execute a full text search using entity framework 6? 回答1: Seems that Entity Framework 6 does not support full text search, but there is a workaround with interceptors. http://www.entityframework.info