-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The property '' on entity type '' has a temporary value while attempting to change the entity's state to 'Modified' #22027
Comments
@cxc256 The code snippets above do not compile and it's not clear what the missing parts should be. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate. |
@ajcvickers , attached is a sample. I used the internal custom repository pattern I'm required to use for the project. |
@cxc256 Thanks for the repro code. Note for team: the issue here is in changing from an Added state to a Modified state when the entity has a temporary FK value. See simplified repro code below. We currently throw, but for an FK it is valid to have a temporary value pointing to a new Added principal. public class SomeDbContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(Your.SqlServerConnectionString);
}
public static class Program
{
public static void Main()
{
using (var context = new SomeDbContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
var post = new Post { Id = 1 };
context.Add(post);
var blog = new Blog();
context.Add(blog);
// Associate with the principal
post.Blog = blog;
// Works if this state change is done before associating the Added principal
// Fails if it is done after, like here.
context.Entry(post).State = EntityState.Modified;
context.ChangeTracker.DetectChanges();
}
}
}
public class Blog
{
public int Id { get; set; }
public ICollection<Post> Posts { get; set; }
}
public class Post
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
public int? BlogId { get; set; }
public Blog Blog { get; set; }
}
|
Related: #14192 |
I have a one to many relationship. I believe I have it set up correctly, please see the code below. A Batch will contain a bunch of Accounts. Everything works correctly when all objects are new and inserted into the database at once. However, there is a user case where Accounts are saved first, before being associated with a Batch. Then later, a Batch is created the existing batchless accounts are added to the Batch. When I try to save, I get the following error: The property 'BatchId' on entity type 'Account' has a temporary value while attempting to change the entity's state to 'Modified'. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property.
I am using ASP.Net Core 3.1 with EF Core 3.1.6 for SQLServer. I'm using Visual Studio 2019 16.6 on Windows 10/2016
Steps to reproduce
4 .Call Batch.AddAccount(account)
The text was updated successfully, but these errors were encountered: