entity-framework

MIN and MAX value by string in Entity Framework

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-29 06:05:53
问题 I have database table. The ORM for it is: public long Id { get; set; } public System.Guid AttrId { get; set; } public System.Guid ProdId { get; set; } public string Value { get; set; } public virtual Attributes Attributes { get; set; } public virtual Products Products { get; set; } As you can see value is a string type. I'm trying to get min and max values by this field (some values represented as double). So here is my method for this: public double[] GetMaxMinVals(Guid attrId) { double[]

Read CSV file and insert to LocalDB (asp.net MVC)

守給你的承諾、 提交于 2021-01-29 05:52:08
问题 I'm trying out a project with ASP.Net MVC and have a large CSV file that I want to save to the LocalDB. I have been following this tutorial (and the ones before that are about MVC): https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/creating-a-connection-string Now I want to add data to this database that I have set up and I would like to read this data from a csv file and then save it to my database. I have tried this: https://www.aspsnippets.com/Articles

Read CSV file and insert to LocalDB (asp.net MVC)

非 Y 不嫁゛ 提交于 2021-01-29 05:51:10
问题 I'm trying out a project with ASP.Net MVC and have a large CSV file that I want to save to the LocalDB. I have been following this tutorial (and the ones before that are about MVC): https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/creating-a-connection-string Now I want to add data to this database that I have set up and I would like to read this data from a csv file and then save it to my database. I have tried this: https://www.aspsnippets.com/Articles

Create Dbcontext at run-time

冷暖自知 提交于 2021-01-29 05:13:24
问题 I have a requirement to switch to a different data sources at runtime. sometimes user wants to connect to a different database server to manipulate data. There can be more than 100 client servers with an identical database. Original project is a .net core Web API and the server name is an input parameter. This is what I thought. Is there a better alternative? public class Person { public int Id { get; set; } public string Name { get; set; } } public class MyDbContext : DbContext { public

Dapper-like Multi Query with EF Core

早过忘川 提交于 2021-01-29 05:11:26
问题 With dapper it is pretty easy to run mulitple queries in one run: string sql = "SELECT * FROM Invoice WHERE InvoiceID = @InvoiceID; SELECT * FROM InvoiceItem WHERE InvoiceID = @InvoiceID;"; using (var connection = My.ConnectionFactory()) { connection.Open(); using (var multi = connection.QueryMultiple(sql, new {InvoiceID = 1})) { var invoice = multi.Read<Invoice>().First(); var invoiceItems = multi.Read<InvoiceItem>().ToList(); } } How to archieve this with EFCore? I found solutions like var

Dapper-like Multi Query with EF Core

家住魔仙堡 提交于 2021-01-29 05:11:16
问题 With dapper it is pretty easy to run mulitple queries in one run: string sql = "SELECT * FROM Invoice WHERE InvoiceID = @InvoiceID; SELECT * FROM InvoiceItem WHERE InvoiceID = @InvoiceID;"; using (var connection = My.ConnectionFactory()) { connection.Open(); using (var multi = connection.QueryMultiple(sql, new {InvoiceID = 1})) { var invoice = multi.Read<Invoice>().First(); var invoiceItems = multi.Read<InvoiceItem>().ToList(); } } How to archieve this with EFCore? I found solutions like var

How can I reference an user object in an Entity Framework model?

可紊 提交于 2021-01-29 04:31:06
问题 I have the following model: public class MyEntity { public Guid Id { get; set; } public virtual ICollection<ApplicationUser> AssociatedUsers { get; set; } public MyEntity() { AssociatedUsers = new HashSet<ApplicationUser>(); } } Notice that each entity has some associated users. In my controller, I'm trying to add an instance of MyEntity to the database like this: private ApplicationDbContext db = new ApplicationDbContext(); // ... ApplicationUser currentUser = System.Web.HttpContext.Current

Calculated Properties in Odata v4 and Entity Framework

末鹿安然 提交于 2021-01-29 04:24:34
问题 Im working on a .Net Web Api 2.2 project and I'm using Odata v4 and Entity Framework code first. I need to have a calculated property in a Model which is not to be saved in Database. I have tried [notmapped] attribute. But it hides the property in Odata $medadata. How can I overcome this issue? 回答1: You can manually add this property into model. You can refer to the sample codes in: How to get Web API OData v4 to use DateTime Hope it can help. 来源: https://stackoverflow.com/questions/31191013

How do I cache lookup data with Entity Framework 6

守給你的承諾、 提交于 2021-01-29 04:21:03
问题 I'm learning my way around EF, and I know caching is faster than a round trip to the DB for things like state, country, etc. But I'm not sure how to implement it. I was looking at this post (entities from local cache) that mentioned an extension, but is there something built in I should leverage? I'd like to have a function like this that wouldn't have to go to the db every time: public static int GetCountryId(string countryCode = "US") { if (countryCode == string.Empty) countryCode = "US"; /

Best way to control unicity of an attribute using EF Code First

霸气de小男生 提交于 2021-01-29 03:32:12
问题 I've got a class User which has an attribute Name that must be unique. So far I've investigated 3 ways of checking this: Annotations [StringLength(100)] [Index(IsUnique = true)] public string Name { get; set; } Problem is, by trying to insert a user with a repeated name it throws this ex: as you can see, I would have to navigate into the inner exceptions (which I don´t know if it is possible, but I assume it is) and the last inner exception´s message is not user friendly at all. Fluent Api