简体   繁体   中英

EF Core ignore property only to INSERT

I'm trying to setup my entity to retrieve data from 2 properties that doesn't exist on the table, basically by doing rawSql. When I mapped these 2 properties at entityBuilder, it just worked fine, but then when I tried to insert new data from this entity, I received an error related the missing columns at the table. When I set Ignore to these properties, it resolved the lack of column error, but stopped retrieving data. Is it possible to ignore columns/properties only to Insert/update direction? In this case, properties PlatformDescription and DriverDescription.

Thanks everyone!

        public OrderMap(EntityTypeBuilder<Order> entityBuilder)
        {
            entityBuilder.HasKey(o => o.Id);
            entityBuilder.Property(o => o.CreationDate).IsRequired();
            entityBuilder.Property(o => o.ModifiedDate);
            entityBuilder.Property(o => o.CreatedBy).IsRequired();
            entityBuilder.Property(o => o.ModifiedBy);
            entityBuilder.Property(o => o.AutomobilePlate);
            entityBuilder.Property(o => o.DriverDriversLicense);
            entityBuilder.Property(o => o.LoginChecking);
            entityBuilder.Property(o => o.LoginPacking);
            entityBuilder.Property(o => o.LoginPicking);
            entityBuilder.Property(o => o.LoginDispatching);
            entityBuilder.Property(o => o.Platform);
            entityBuilder.Property(o => o.TrackingCode).IsRequired();
            entityBuilder.Property(o => o.PlatformDescription);
            entityBuilder.Property(o => o.DriverDescription);
            entityBuilder.Ignore("PlatformDescription");
            entityBuilder.Ignore("DriverDescription");
        }
var query = "SELECT A.Id ";
                query += ",A.AutomobilePlate ";
                query += ",A.CreatedBy ";
                query += ",A.CreationDate ";
                query += ",A.DriverDriversLicense ";
                query += ",A.LoginChecking ";
                query += ",A.LoginDispatching ";
                query += ",A.LoginPacking ";
                query += ",A.LoginPicking ";
                query += ",A.ModifiedBy ";
                query += ",A.ModifiedDate ";
                query += ",A.Platform ";
                query += ",A.Status ";
                query += ",A.TrackingCode ";
                query += ",B.Description PlatformDescription ";
                query += ",B.Complement ";
                query += ",C.Description DriverDescription ";
                query += " FROM [Order] A ";
                query += " LEFT JOIN Setup B ";
                query += "   ON A.Platform = B.Id ";
                query += " LEFT JOIN Setup C ";
                query += "   ON A.DriverDriversLicense = C.Id ";
                query += "WHERE A.CreationDate > DATEADD(DAY, -30, GETDATE()) ";

                return entities.FromSqlRaw(query).ToList();

Given your sql query;

SELECT A.Id
,A.AutomobilePlate
,A.CreatedBy
,A.CreationDate
,A.DriverDriversLicense
,A.LoginChecking
,A.LoginDispatching
,A.LoginPacking
,A.LoginPicking
,A.ModifiedBy
,A.ModifiedDate
,A.Platform
,A.Status
,A.TrackingCode
,B.Description PlatformDescription
,B.Complement
,C.Description DriverDescription
 FROM [Order] A
 LEFT JOIN Setup B
   ON A.Platform = B.Id
 LEFT JOIN Setup C
   ON A.DriverDriversLicense = C.Id
WHERE A.CreationDate > DATEADD(DAY, -30, GETDATE())

It looks like you really just want to define 2 entities with their associated navigation properties. That way you don't need to use raw sql at all;

public class Order {
    public int Id { get; set; }
    public int? Platform { get; set; }
    public int? DriverDriversLicense { get; set; }
    // snip
    public virtual Setup SetupPlatform { get; set; }
    public virtual Setup SetupDriver { get; set; }
}

public class Setup {
    public int Id { get; set; }
    public string Description  { get; set; }
    // snip
}

public OrderMap(EntityTypeBuilder<Order> entityBuilder)
{
// snip
entityBuilder
    .HasOne(e => e.SetupPlatform)
    .WithOne()
    .HasForeignKey<Order>(e => e.Platform);
entityBuilder
    .HasOne(e => e.SetupDriver)
    .WithOne()
    .HasForeignKey<Order>(e => e.DriverDriversLicense);
}

It is possible to force each Setup navigation property to be loaded whenever the Order is loaded;

.HasForeignKey ... 
.Metadata.PrincipalToDependent.SetIsEagerLoaded(true);

And define extra properties, for convenience, that aren't mapped to the Order table in the database;

public class Order {
    [NotMapped]
    public string PlatformDescription => Platform?.Description;
    [NotMapped]
    public string DriverDescription => SetupDriver?.Description;
...

Just use [NotMapped] for your 2 new properties that don't exist in this table. For example, Order Model class,

public class Order
{
    [NotMapped]
    public string NewProperty1 { get; set; }
    [NotMapped]
    public string NewProperty2 { get; set; }
}

EDITED

在此处输入图像描述

在此处输入图像描述

You can use this linq and change these tables and columns to your own tables and columns.

var setup = db.TSetup;
var list = (from a in db.TOrder
            from platform in setup.Where(x => x.Id == a.Platform).DefaultIfEmpty()
            from driver in setup.Where(x => x.Id == a.DriverDriversLicense).DefaultIfEmpty()
            select new TOrder()
            {
                Id = a.Id,
                Name = a.Name,
                PlatformDescription = platform.Description,
                DriverDescription = driver.Description
            }).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM