Closed netyam closed 5 years ago
@netyam This is an issue with read-only fields. As a workaround, define the fields explicitly and make them not read-only. For example:
private int _id;
public int Id => _id;
For triage: more minimal repro:
public class Pizza
{
public Pizza() { }
public Pizza(int id, string name, decimal price)
{
Id = id;
Name = name ?? throw new ArgumentNullException(nameof(name), "A pizza needs a name!");
Price = price;
}
public int Id { get; }
public string Name { get; }
public decimal Price { get; }
}
public class PizzaPlaceDbContext : DbContext
{
public DbSet<Pizza> Pizzas { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var pizzaEntity = modelBuilder.Entity<Pizza>();
pizzaEntity.HasKey(pizza => pizza.Id);
pizzaEntity.Property(pizza => pizza.Price).HasColumnType("money");
pizzaEntity.Property(pizza => pizza.Name);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
}
public class Program
{
public static void Main()
{
using (var context = new PizzaPlaceDbContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.Add(new Pizza(0, "Cheese", 1.0m));
context.SaveChanges();
}
using (var context = new PizzaPlaceDbContext())
{
foreach (var pizza in context.Pizzas)
{
Console.WriteLine(pizza.Name);
}
}
}
}
@netyam My book "Blazor Revealed" mentions that you should make the properties read-write now :) Actually funny that I encounter this here (since I am experimenting with the latest EF Core). Greetings from Belgium!
Fixed in #15995
Regression test Query_and_update_using_constructors_with_property_parameters
Hey i have a question, i tried to migrate to a database with
Microsoft.EntityFrameworkCore.SqlServer 3.0.0-preview6.19304.10 Microsoft.EntityFrameworkCore.Tools 3.0.0-preview6.19304.10 .NET Core 3.0 SDK 3.0.100-preview6-012264
i used PM> add-migration PM> update-database Migration was okay but when i try to open it through /api/pizzas/getpizzas i got this problem:
To Reproduce
Using Microsoft.EntityFrameworkCore.SqlServer 3.0.0-preview6.19304.10 Microsoft.EntityFrameworkCore.Tools 3.0.0-preview6.19304.10 .NET Core 3.0 SDK 3.0.100-preview6-012264
Run this code:
Pizza.cs
PizzaController.cs
PizzaDbContext.cs
Startup.cs
Migration
Expected behavior
Just json of pizzas