It's 2021, why would you use ADO.NET and Dapper (SQLish styles) over EF ?
Is EF STILL slower than ADO.NET and DAPPER ?
Hello. Anyone done any benchmarking recently, is EF Core STILL slower than ADO.NET and DAPPER like the analysis done in this article ? Dapper vs Entity Framework vs ADO.NET Performance Benchmarking (exceptionnotfound.net)
Razor pages and database with a specific schema
Hi, I'm building an application for already existing DB. Within a DB there are several schemas eg:
- dbo
- Production,
- Purchasing,
- ...
I don't have any problems to work with dbo schema, because there is no neccesity to refer to dbo anywhere eg:
public DbSet<Employee> Employee { get; set; } that dbo.Employee table
But what would I have to do to connect with Production.Product table?
Azure Web Job Template in VS2019
Is it possible to add a LINQ2SQL class (DBML) to an Azure Web Job project template? I have been trying this for a couple of hours with zero luck. I am able to add this to a console application or a regular ASP .NET project.
There are no errors, but the cursor keeps spinning forever. A 1kb file is created in the folder, but the file is completely unusable. Any idea why this would happen would definitely help.
Getting data from Stored procedure that returns multiple result sets using Entity Framework in MVC
I am rebuilding a project in MVC using Entity Framework, which was originally built using web forms. One of the pages needs 'Employee Details' that are spread over 5 sub tables that a stored procedure returns. I am using Entity Framework for views. Is it possible to use the same stored procedure with EF? Can someone provide a link that explains how to do this. So far I have been able to get data from a SP that returns a single dataset. But a SP that uses multiple tables and returns multiple subsets using EF seems to want a different way. Let me know. Thanks!
JSON_Value error: The argument 2 of the “JSON_VALUE or JSON_QUERY” must be a string literal (SQL 2016)
Hello,
I'm seeing this error when running the code on SQL 2016. On SQL 2017 it works fine.
Argument 2 of the "JSON_VALUE or JSON_QUERY" must be a string literal.
I updated the 2nd argument to a string literal and it fixed the error and works fine.
But am I open to SQL Injection now due to this? Can someone explain?
Working Code:
public async Task<IList<FileSystemItemJsonDTO>> GetFileSystemItems(int moduleId, IDictionary<string, string> metadata) { var sqlParams = new List<SqlParameter>(); StringBuilder sb = new StringBuilder(); // start the initial select query... sb.Append("SELECT * FROM dbo.FileSystemItems WHERE "); int counter = 0; foreach (var item in metadata) { // only add an AND if we are NOT the first record... if (counter != 0) { sb.Append(" AND "); } // setup our json path and value items... string valueParam = string.Format(CultureInfo.CurrentCulture, "jsonPathValue{0}", counter); // 2nd item for JSON_VALUE has to be string literal for SQL server 2016 sb.AppendFormat(CultureInfo.CurrentCulture, "JSON_VALUE(FileMetadata, '$.{0}') = @{1}", item.Key, valueParam); // add in our parameters to assist with sql injection sqlParams.Add(new SqlParameter(valueParam, string.Format(CultureInfo.CurrentCulture, "{0}", item.Value))); counter++; } return await BIContext.FileSystemItems .Where(x => x.ModuleId == moduleId) .FromSql(sb.ToString(), sqlParams.ToArray()) .Select(s => new FileSystemItemJsonDTO { FileId = s.FileId, FileName = s.FileName, FileType = s.FileType, LastWriteTime = s.LastWriteTime, FileSystemItemDataId = s.FileSystemItemDataId, ModuleId = moduleId, FileMetadata = s.FileMetadata, FileSize = s.FileSize }) .ToListAsync().ConfigureAwait(false); }
Failing Code:
public async Task<IList<FileSystemItemJsonDTO>> GetFileSystemItems(int moduleId, IDictionary<string, string> metadata) { var sqlParams = new List<SqlParameter>(); StringBuilder sb = new StringBuilder(); // start the initial select query... sb.Append("SELECT * FROM dbo.FileSystemItems WHERE "); int counter = 0; foreach (var item in metadata) { // only add an AND if we are NOT the first record... if (counter != 0) { sb.Append(" AND "); } // setup our json path and value items... string pathParam = string.Format(CultureInfo.CurrentCulture, "jsonPathParam{0}", counter); string valueParam = string.Format(CultureInfo.CurrentCulture, "jsonPathValue{0}", counter); sb.AppendFormat(CultureInfo.CurrentCulture, "JSON_VALUE(FileMetadata, @{0}) = @{1}", pathParam, valueParam); // add in our parameters to assist with sql injection sqlParams.Add(new SqlParameter(pathParam, string.Format(CultureInfo.CurrentCulture, "$.{0}", item.Key))); sqlParams.Add(new SqlParameter(valueParam, item.Value)); counter++; } return await BIContext.FileSystemItems .Where(x => x.ModuleId == moduleId) .FromSql(sb.ToString(), sqlParams.ToArray()) .Select(s => new FileSystemItemJsonDTO { FileId = s.FileId, FileName = s.FileName, FileType = s.FileType, LastWriteTime = s.LastWriteTime, FileSystemItemDataId = s.FileSystemItemDataId, ModuleId = moduleId, FileMetadata = s.FileMetadata, FileSize = s.FileSize }) .ToListAsync().ConfigureAwait(false); }
Notice the difference on this line:
// 2nd item for JSON_VALUE has to be string literal for SQL server 2016 sb.AppendFormat(CultureInfo.CurrentCulture, "JSON_VALUE(FileMetadata, '$.{0}') = @{1}", item.Key, valueParam);
Linq FromSqlRaw - Data is Null. - Comments Field Varchar Null
Hi All,
I got this;
var result = _context.Set<spCallResult>().FromSqlRaw("exec [dbo].[spCallVal] @XX={0}", xx).ToList();
System.Data.SqlTypes.SqlNullValueException: 'Data is Null. This method or property cannot be called on Null values.'
My Comments field is varchar(max) null
How to resolved this in FromSqlRaw?
Thanks
Regards,
Micheale
how to calculate time taken by cmd.ExecuteReader.
hi, I have sqlserver 2019 and asp.net 2019 and iis 7 on the same machine.
What I have noticed is a stored procedure takes 19 second from query window of sqlserver,
when I run it first time. second time it takes 10 sec because at that time cache is built.
Q1) i have called the sp third time from cmd.ExecuteReader and it takes 18 sec.
so please tell me the delay of 7 to 8 sec of cmd.ExecuteReader is correct or we can speed up.
the data returned by sp is only 20 columns and 30 rows.
yours sincerley
adding additional fields to EF dataset in View and submitting
I am new to MVC, EF. My MVC project has a view that displays data that I get via EF. So, that model subset is already created. But after the user sees this form, I need to add an additional field 'COMMENTS' to his form, and have the user submit it. I created a separate Model for this 'comments' field, but am unable to include more than 1 model in this view page, something like this (which will look as a huge blunder to people who know this well, I am not one of them yet :) )
<div>@model IEnumerable<Admin_Tool.Func_EmpDetails_Result></div> <div>@model Admin_Tool.Models.Form</div> <div></div> <div>I think the best way would be to create another Model Class with these 2. Let me know if this is correct? Or is there a better way.</div> <div></div> <div>Thanks!</div> <div>-RDesh</div>Using EF Core entity if child view also exists throws error
ASP.NET 5 MVC Shopping cart application uses EF Core with Npgsql data provider.
Database contains products table
create table Product ( Product char(20) primary key; Description char(50); ) It is mapped to Product entity by EF Core Scaffold public class Product { public string Product1 { get; set; } public string Description { get; set; } } Read-only product view entity has special shadow properties which do not exist in database and in Product class: public class ShopToode: Product { public decimal CartPrice { get; set; } } public DbSet<ShopToode> ShopToodes { get; set; } public DbSet<Product> Products { get; set; } ShopToode is used only to view data using FromSqlRaw: var tooteinfo = await ctx.ShopToodes.FromSqlRaw(@"select *, 1.2 as CartPrice from Product").AsNoTracking().ToListAsync(); Trying to get product like var t = ctx.Products.First(); throws error > Npgsql.PostgresException (0x80004005): 42703: column t.Discriminator> does not exist How to use Product entity if view also exists ?
Linq - does not contain a definition for 'Contains'
I have an array of ServiceGrid items called services, the relevant properties are shown below:
svcId / svcName / svcBillingRate 5 / serviceA / 10.00 19 / serviceB / 19.25 11 / serviceC / 24.00 17 / serviceD / 4.50 2 / serviceE / 9.00
also have a hashset<int> addOnOptions that has a number of svcIds, say 11 and 17
My objective is to end up with:
5 / serviceA / 10.00 19 / serviceB / 19.25 2 / serviceE / 9.00
var theSelectedAddOns = addOnOptions.ToList().Where(x => new[] {services.ToList()}.Contains(x));
This gives the following errors:
<div>'List<ServiceGrid>[]' does not contain a definition for 'Contains' and the best extension method overload 'MemoryExtensions.Contains<int>(ReadOnlySpan<int>, int)' requires a receiver of type 'ReadOnlySpan<int>'</div>In SQL this would be very easy:
select * from services where svcId in (5, 19, 2)
Looking for ideas on how to proceed.
How to use async scaffold and compile
ASP.NET 5 MVC Core application uses EF Core Npgsql runtime scaffold and Roslyn compile to different versions of EF Code DbContext assembly.
It takes 12 seconds to scaffold database. So async operations are preferred.
Scaffold interface does not have async versions:
namespace Microsoft.EntityFrameworkCore.Scaffolding { public interface IReverseEngineerScaffolder { SavedModelFiles Save([NotNullAttribute] ScaffoldedModel scaffoldedModel, [NotNullAttribute] string outputDir, bool overwriteFiles); ScaffoldedModel ScaffoldModel([NotNullAttribute] string connectionString, [NotNullAttribute] DatabaseModelFactoryOptions databaseOptions, [NotNullAttribute] ModelReverseEngineerOptions modelOptions, [NotNullAttribute] ModelCodeGenerationOptions codeOptions); } } Roslun Create method which compiles also dont have async version: namespace Microsoft.CodeAnalysis.CSharp { public sealed class CSharpCompilation : Compilation { public static CSharpCompilation Create(string? assemblyName, IEnumerable<SyntaxTree>? syntaxTrees = null, IEnumerable<MetadataReference>? references = null, CSharpCompilationOptions? options = null); public static CSharpCompilation CreateScriptCompilation(string assemblyName, SyntaxTree? syntaxTree = null, IEnumerable<MetadataReference>? references = null, CSharpCompilationOptions? options = null, CSharpCompilation? previousScrip ... How to perform async scaffold and compile in EF Core ?
Member AutoSync failure from LINQ
My application generated the following error message recently
System.InvalidOperationException: Member AutoSync failure. For members to be Auto-Synced after insert, the type must either have an auto-generated identity, or a key that is not modified by the database after insert at System.Data.Linq.ChangeDirector.StandardChangeDirector.DynamicInsert(TrackedObject item) at System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert(TrackedObject item) at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) at System.Data.Linq.DataContext.SubmitChanges() at UserLogManager.Add(String logDir, String connectionString, String workStationId, LogType logType, String source, LogAction logAction, String detail, String userId)
In the dbml, I have not found property IsDbGenerated and Auto-Sync on this table, the following column is primary key
[Column(Storage = "_gLogID", DbType = "UniqueIdentifier NOT NULL", IsPrimaryKey = true)]
The error happens sometimes, but not always.
Any idea to troubleshoot the problem?
Thanks,
Tony
issues in defining relationship between entities in EntityFramework core code first
Hi Aall,
I am completely new to Entity framework, working on sample app.
Following the .entity framework CodeFirst approach for asp.net core web api
we created two entities
public class Account { public int AccountId { get; set; } public int AccountNumber { get; set; } public DateTime CreatedDate { get; set; } public DateTime ModifiedDate { get; set; } public decimal Balance { get; set; } public int AccountTypeId { get; set; } public AccountType AccountType { get; set; } }
public class AccountType { public int AccountTypeId { get; set; } public string Type { get; set; } public string Name { get; set; } public string Description { get; set; } public DateTime CreatedDate { get; set; } public DateTime ModifiedDate { get; set; } public List<Account> Accounts { get; set; } }
These are the two entities to for simulating a bank account
We have created a APi endpint for Creating account like below
[HttpPost] public async Task<ActionResult<Account>> CreateAccount(Account account) { return await _accountService.AddAccountAsync(account); }
But the problem is the swagger is showing the parameter like below
{"accountId": 0,"accountNumber": 0,"createdDate": "2021-04-19T04:08:22.058Z","modifiedDate": "2021-04-19T04:08:22.058Z","balance": 0,"accountTypeId": 0,"accountType": {"accountTypeId": 0,"type": "string","name": "string","description": "string","createdDate": "2021-04-19T04:08:22.058Z","modifiedDate": "2021-04-19T04:08:22.058Z","accounts": ["string" ] } }
how do I pass only the account details for creation ? because it has both account and accountype fields are present
am doing anything wrong while defining the relationship between entities.
My intention is to reference the AccountTypeId mentioned in the account so that it wont insert accountypeid other than in the accountype table
please correct me if I am doing anything wrong
thanks
Sorting SQL Adapter
Hi,
I have this SQL adapter taking an id from a previous page but i want to be able to sort it by the Year field any ideas?
New SqlDataAdapter("SELECT Artist_Code, Artist, Title, Album_Code, Year, Tracks FROM tbl_Artist_Albums WHERE (Length=1 OR Length=2) AND Artist_Code=" + _
Request.QueryString("id"), objConn)
EF Core - Data lost when unit testing with in-memory database, but works fine on the real database
I have some code that saves a Term
(as in an investor, who invests in property for a fixed term). Each term has an associated collection of shares, one for each property...
publicclassTerm {publicTerm() =>
Shares = new();publicint Id { get; set; }publicstring InvestorId { get; set; }// Other properties removed for claritypublic List<Share> Shares { get; set; }
}publicclassShare {publicint Id { get; set; }publicint TermId { get; set; }public Term Term { get; set; }publicint PropertyId { get; set; }public Property Property { get; set; }publicint Quantity { get; set; }
}
My view model has an _investor
property. When a term is updated, say if the investor changes the number of shares they have, the change is made, and then saved. After that, a method is called that calculates the balance
brought forward from the previous term (ie, how much they over/underpaid)...
privateasync Task SetBalanceBroughtForward() {
Term previousTerm = _investor.Terms.OrderByDescending(t => t.Start).Skip(1).First();
Term latestTerm = _investor.Terms.OrderByDescending(t => t.Start).First();
latestTerm.BalanceBroughtForward = CalculateBbf(previousTerm);
_appDbContext.Terms.Update(latestTerm);await _appDbContext.SaveChangesAsync();
}
This code works fine when running locally (ie from Visual Studio), but fails unit testing. I have a test that tests the scenario when the investor has an extra share added to the term. Here is the test code...
[TestMethod]publicasync Task TestMethodNameShortenedForClarity() {// GetInvestorDetailsViewModel sets up the test view model,// passing in the in-memory db context and other dependencies
InvestorDetailsViewModel investorDetailsVm = GetInvestorDetailsViewModel();// Tell the VM to get the investor from the database and set various VM properties// The investor used in this test has 6 shares in one property in the current termawait investorDetailsVm.SetInvestor(_investorId);// Get a VM for the renewal window
RenewalViewModel newRenewalVm = await investorDetailsVm.NewRenewalViewModel();// Add 7 shares for a second property to the current term
Share addedShare = new() { PropertyId = _property2.Id, Quantity = 7 };
newRenewalVm.Shares.Add(addedShare);// Call the VM method to do the renewal. This calls the code shown earlierawait investorDetailsVm.OnSubmitRenew(newRenewalVm);// Get the modified investor from the database
Investor modifiedInvestor = await _appDbContext.Investors
.SingleAsync(i => i.Id.ToString() == _investorId);// Test the changes. All removed for clarity except the failing one
Assert.AreEqual(2, latestTerm.Shares.Count);
}
If I use tracepoints to check the number of shares in the latest term, I can see that just before the call to SaveChangesAsync
, it has two shares as expected, but straight after that call, the newly-added share has disappeared.
The _investor
has been reloaded from the database after the share was added (in the renewal code), before SetBalanceBroughtForward
was called, and the new share was there.
Just as a check, I tried changing the code in SetBalanceBroughtForward
to get the latest term from the database...
Term latestTerm = await _appDbContext.Terms.Where(t => t.InvestorId==_investor.Id)
.OrderByDescending(t => t.Start).FirstAsync();
...but it didn't make any difference. latestTerm
had two shares before calling SaveChangesAsync
but only one afterwards.
Anyone any idea why this newly-added share gets removed? Thanks
EF Core NotSupportedException The property does not have a value set and no value generator is available for properties of type decimal. Either set a value for the property before adding the entity
UPDATE: Could not figure out how the autoincrement of decimal works for Unit Testing. The workaround is
[TestMethod]
public void Add()
{
MyEntityRepository = new MyEntityRepository (context);
myEntityObject.id - 1; // workaround
int result = MyEntityRepository.Add(myEntityObject); // throw error here
}
HI,
I am using EF Core 3.16 and using Unit Test to try to add a Entity object with id = 0 (decimal type) but it is throwing a NotSupportedException error
EF Core NotSupportedException The property does not have a value set and no value generator is available for properties of type decimal. Either set a value for the property before adding the entity
I tried using ValueGenerator and IsInMemory approach in my dbContext.cs but I noticed on the microsoft doc
it is v5.0 but my app is using v3.16
The exception error has a phase to set the value which already contains an id=0 but it still throws the error. When running without TestMethod, the EF Core Add works with no issue but the error is happening in unit test
Here is my code so far, any help is appreciated
public MyTest()
{
var options = new DBContextOptionsBuilder<MyDbContext>().UseInMemoryDatabase(databaseName: "MyDB").Options;
context = new MyDbContext(options);
context.Database.EnsureDeleted();
}
[TestMethod]
public void Add()
{
MyEntityRepository = new MyEntityRepository (context);
int result = MyEntityRepository.Add(myEntityObject); // throw error here
}
in my repository code
public async Task<decimal> Add(MyEntity obj)
{
await this.MyDbContext.MyEntity.AddAsync(obj);
await this.MyDbContext.SaveChangesAsync();
return obj.Id;
}
Convert linq to sql query
Hi , I struggle to convert a query from linq to sql ,
my query is:
from box in _context.MailIteboxStore.Where(x => x.boxId == boxId) .Include(x => x.boxItem).ThenInclude(x => x.boxItemGen) .Include(x => x.boxItem).ThenInclude(x => x.boxItemQ).ThenInclude(x => x.Qualifing).ToList() join itm in _context.ItemStore .Include(x => x.occurrence).ThenInclude(x => x.ExternalKey) .Include(x => x.occurrenceType) .Include(x => x.Store) on box.boxItemId equals itm.itmId join c in _context.occurrenceAttribute.Where(x => x.Attribute.Code == "NEW") .Include(y => y.AttributeValue).ThenInclude(y => y.State) .Include(y => y.Attribute) on itm.occurrenceId equals c.occurrenceId into Left from l in Left.DefaultIfEmpty() select new boxItemBoxDetails { boxItem = box.boxItem, ItemStore = itm, }
I cannot able to render several join and left join to correct sql
Can anyone help me?
Thanks
The instance of entity type cannot be tracked because another instance with the same key value for {'ID'} is already being tracked.
The instance of entity type 'Table' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.
Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.
This is the message I received when trying to write multiple records to database.
My codes work perfectly fine when there is only one record to be written to database.
So, this situation is quite weird.
Here is my code snippet
var newcompany = new Company();
newcompany.CompanyName = Name;
newcompany.Address = Address;
newcompany.Phone = Phone;
newcompany.City = City;
newcompany.Country = Country;
newcompany.Employees = 100;
newcompany.State = State;
_context.Entry(newcompany).State = EntityState.Modified;
_context.Dividend.Add(newcompany);
await _context.SaveChangesAsync();
Please notice that when there is one single record to be written into database, it works perfectly fine.
It only throws an exception when there are multiple records to be written into database.
Thank you for any suggestions.
EF Core, first query is slow, how to generate a precompile view to improve performance
Hi,
I am working on webapi 3.1 with entityframework core 3.16. I got about 180 entity that is slow with the first query. I read that precompiled entityframework view can help improve performance. I watched a video,https://www.youtube.com/watch?v=qsm12syxRWs
After watching it, I downloaded EF Core Power Tools and EntityFramework Power Tools in the Extension menu. When I right clicked on my MyModelDbContext.cs, I don't see the "Optimize Entity Model" option. I just see Generate Views which did not generate any file. When I right clicked on the Repository C# Library project, I see EF Core Tools but it has no options about Optimizing the data model.
How can this be done? Thanks,