admin管理员组文章数量:1130349
I have an ASP.NET Core Web API endpoint, a SQL Server stored procedure is called to get the data.
Amongst the columns of the resulting DbSet<> the n is the counter with integer value (not NULL), and the mnozstvi (quantity) defined as numeric(19, 4), with NULL allowed.
The stored procedure is called like this:
public async Task<List<PolozkaKosiku>> GetAllAsync(string kodZakaznika)
{
SqlParameter kodz = new SqlParameter("@kod_zakaznika", kodZakaznika);
var polozkyKosiku = dbContext.Kosik
.FromSql($"EXECUTE usp_kosik @kod_zakaznika={kodz}")
.ToListAsync();
return await polozkyKosiku;
}
The PolozkaKosiku (BasketItem) defines the model
public class PolozkaKosiku
{
[Column(TypeName = "int")]
public int n { get; }
[Column(TypeName = "numeric(19, 4)")]
public decimal? Mnozstvi { get; set; }
}
There is also the related PolozkaKosikuDto class and the AutoMapper definition.
The Swagger UI shows the data were retrieved successfully
However, the n is always 0. When checking the results of calling the stored procedure the mnozstvi shows the expected values. The n correctly numbers the rows:
I am stuck. Being new to ASP.NET Core Web API and EF, I do not know where to search for the bug.
One my guess is that it may be related to the modelBuilder. Earlier, the result was taken directly from the table. However, the stored procedures was introduced to enhance the records by related information, including the n as the order of the item. Also, the kosik table has the key ID, not the n (the n is not inside the table):
modelBuilder.Entity<PolozkaKosiku>()
.ToTable("kosik")
.HasKey(new string[] { "n" });
The n was declared as a key only to make the code working somehow.
Where is the bug?
I have an ASP.NET Core Web API endpoint, a SQL Server stored procedure is called to get the data.
Amongst the columns of the resulting DbSet<> the n is the counter with integer value (not NULL), and the mnozstvi (quantity) defined as numeric(19, 4), with NULL allowed.
The stored procedure is called like this:
public async Task<List<PolozkaKosiku>> GetAllAsync(string kodZakaznika)
{
SqlParameter kodz = new SqlParameter("@kod_zakaznika", kodZakaznika);
var polozkyKosiku = dbContext.Kosik
.FromSql($"EXECUTE usp_kosik @kod_zakaznika={kodz}")
.ToListAsync();
return await polozkyKosiku;
}
The PolozkaKosiku (BasketItem) defines the model
public class PolozkaKosiku
{
[Column(TypeName = "int")]
public int n { get; }
[Column(TypeName = "numeric(19, 4)")]
public decimal? Mnozstvi { get; set; }
}
There is also the related PolozkaKosikuDto class and the AutoMapper definition.
The Swagger UI shows the data were retrieved successfully
However, the n is always 0. When checking the results of calling the stored procedure the mnozstvi shows the expected values. The n correctly numbers the rows:
I am stuck. Being new to ASP.NET Core Web API and EF, I do not know where to search for the bug.
One my guess is that it may be related to the modelBuilder. Earlier, the result was taken directly from the table. However, the stored procedures was introduced to enhance the records by related information, including the n as the order of the item. Also, the kosik table has the key ID, not the n (the n is not inside the table):
modelBuilder.Entity<PolozkaKosiku>()
.ToTable("kosik")
.HasKey(new string[] { "n" });
The n was declared as a key only to make the code working somehow.
Where is the bug?
Share Improve this question edited Feb 26 at 15:11 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked Feb 26 at 13:35 peprpepr 20.8k15 gold badges82 silver badges144 bronze badges 2 |1 Answer
Reset to default 1The property n in class PolozkaKosiku has no setter. EF can't set the value for read only property.
Modify your class
public class PolozkaKosiku
{
[Column(TypeName = "int")]
public int n { get; set; }
I have an ASP.NET Core Web API endpoint, a SQL Server stored procedure is called to get the data.
Amongst the columns of the resulting DbSet<> the n is the counter with integer value (not NULL), and the mnozstvi (quantity) defined as numeric(19, 4), with NULL allowed.
The stored procedure is called like this:
public async Task<List<PolozkaKosiku>> GetAllAsync(string kodZakaznika)
{
SqlParameter kodz = new SqlParameter("@kod_zakaznika", kodZakaznika);
var polozkyKosiku = dbContext.Kosik
.FromSql($"EXECUTE usp_kosik @kod_zakaznika={kodz}")
.ToListAsync();
return await polozkyKosiku;
}
The PolozkaKosiku (BasketItem) defines the model
public class PolozkaKosiku
{
[Column(TypeName = "int")]
public int n { get; }
[Column(TypeName = "numeric(19, 4)")]
public decimal? Mnozstvi { get; set; }
}
There is also the related PolozkaKosikuDto class and the AutoMapper definition.
The Swagger UI shows the data were retrieved successfully
However, the n is always 0. When checking the results of calling the stored procedure the mnozstvi shows the expected values. The n correctly numbers the rows:
I am stuck. Being new to ASP.NET Core Web API and EF, I do not know where to search for the bug.
One my guess is that it may be related to the modelBuilder. Earlier, the result was taken directly from the table. However, the stored procedures was introduced to enhance the records by related information, including the n as the order of the item. Also, the kosik table has the key ID, not the n (the n is not inside the table):
modelBuilder.Entity<PolozkaKosiku>()
.ToTable("kosik")
.HasKey(new string[] { "n" });
The n was declared as a key only to make the code working somehow.
Where is the bug?
I have an ASP.NET Core Web API endpoint, a SQL Server stored procedure is called to get the data.
Amongst the columns of the resulting DbSet<> the n is the counter with integer value (not NULL), and the mnozstvi (quantity) defined as numeric(19, 4), with NULL allowed.
The stored procedure is called like this:
public async Task<List<PolozkaKosiku>> GetAllAsync(string kodZakaznika)
{
SqlParameter kodz = new SqlParameter("@kod_zakaznika", kodZakaznika);
var polozkyKosiku = dbContext.Kosik
.FromSql($"EXECUTE usp_kosik @kod_zakaznika={kodz}")
.ToListAsync();
return await polozkyKosiku;
}
The PolozkaKosiku (BasketItem) defines the model
public class PolozkaKosiku
{
[Column(TypeName = "int")]
public int n { get; }
[Column(TypeName = "numeric(19, 4)")]
public decimal? Mnozstvi { get; set; }
}
There is also the related PolozkaKosikuDto class and the AutoMapper definition.
The Swagger UI shows the data were retrieved successfully
However, the n is always 0. When checking the results of calling the stored procedure the mnozstvi shows the expected values. The n correctly numbers the rows:
I am stuck. Being new to ASP.NET Core Web API and EF, I do not know where to search for the bug.
One my guess is that it may be related to the modelBuilder. Earlier, the result was taken directly from the table. However, the stored procedures was introduced to enhance the records by related information, including the n as the order of the item. Also, the kosik table has the key ID, not the n (the n is not inside the table):
modelBuilder.Entity<PolozkaKosiku>()
.ToTable("kosik")
.HasKey(new string[] { "n" });
The n was declared as a key only to make the code working somehow.
Where is the bug?
Share Improve this question edited Feb 26 at 15:11 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked Feb 26 at 13:35 peprpepr 20.8k15 gold badges82 silver badges144 bronze badges 2- 1 Just use EF Core Power Tools to create the correct mapping for you – ErikEJ Commented Feb 26 at 13:54
-
Go figure, if the stored procedure doesn't return
nthen how should it get a value? – Gert Arnold Commented Feb 26 at 16:24
1 Answer
Reset to default 1The property n in class PolozkaKosiku has no setter. EF can't set the value for read only property.
Modify your class
public class PolozkaKosiku
{
[Column(TypeName = "int")]
public int n { get; set; }
本文标签:
版权声明:本文标题:entity framework - ASP.NET Core Web API, EF, data from SQL stored procedure...: why the zero value is returned for the number of 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1741121645a1832481.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


nthen how should it get a value? – Gert Arnold Commented Feb 26 at 16:24