Within EF core 3.1, I am trying to run some custom SQL as a test, to see if I can populate a custom object. My custom object is called CustomPerson.
public partial class CustomPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
The code to run the SQL is as follows:
public List<CustomPerson> GetCustomPersonSql()
{
var persons = _context.CustomPerson.FromSqlRaw("SELECT FirstName, LastName FROM Person");
return persons.ToList();
}
I have declared it as follows in the Context file, note the HasNoKey().
public DbSet<CustomPerson> CustomPerson { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<CustomPerson>(entity =>
{
entity.HasNoKey();
});
}
This works and returns the data, but when I run the following migration to check if it thinks anything has changed.
Add-Migration anythingNew
It creates the CustomPerson table! which I don't want. This needs to be a query only object and not stored or added to the database.
I have tried the following instead in the OnModelCreating, but this then stops the query from working.
modelBuilder.Ignore<CustomPerson>();
Can anyone help me get this working. So I want it to be used to query custom SQL responses, but not be included in any migrations?