Entity Framework: Remove Both Directional Navigation when Scaffolding

Viewed 372

When I scaffold a database with Entity Framework Core, how do I prevent bidirectional properties ? I only want to allow 1 way unidirectional navigation. Is there a command line scaffold parameter?

In Product Class,I want to remove this property

public virtual ICollection<CustomerTransaction> CustomerTransaction { get; set; }

Basically only Customer Transaction should have Navigation, since it has the actual Foreign Key. Want to remove last line, and have similarly 1000+ table entities like this. Need EF scaffold command line parameter or automation (maybe with Regex or other tool).

Database:

create table dbo.CustomerTransaction(
    CustomerTransactionId int NOT NULL,
    ProductId int NULL,
    Quantity int NULL,
    CustomerLastName varchar(255) NULL,
    constraint PK_CustomerTransactionId primary key clustered (CustomerTransactionId ASC))

alter table dbo.CustomerTransaction add constraint fk_CustomerTransaction_Product_ProductId FOREIGN KEY(ProductId) REFERENCES dbo.Product (ProductId)

Entity Framework:

public partial class CustomerTransaction
{
    public int CustomerTransactionId { get; set; }
    public int? ProductId { get; set; }
    public int? Quantity { get; set; }
    public string CustomerLastName { get; set; }

    //Keep this line since it has the foreign key
    public virtual Product Product { get; set; }
}


public partial class Product
{
    public Product()
    {
        CustomerTransaction = new HashSet<CustomerTransaction>();
    }

    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public decimal? Amount { get; set; }

    // REMOVE THIS LINE
    public virtual ICollection<CustomerTransaction> CustomerTransaction { get; set; }
}

Result:

Only want this way available,

storeDbContext.CustomerTransaction.Include(c => c.Product).ToListAsync()

Not this way,

storeDbContext.Product.Include(c => c.CustomerTransaction).ToListAsync()

Using EF Core 2.2,

0 Answers
Related