efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Consider allowing table mapping by attribute on the `DbSet`

Open ajcvickers opened this issue 7 years ago • 5 comments

In #15746 is is suggested that we allow TableAttribute on DbSet properties. Using Table for this is problematic as described there, but we could allow another attribute to be used for this.

Also, we should consider this along with #15241 which is also about attributing the DbSet property for a similar reason.

ajcvickers avatar May 17 '19 20:05 ajcvickers

Looks nice, but there are another real case scenario which is not covered by this feature: Tables may contain archived data splitted by years

GOODS_1995
GOODS_1996
...

So, based on year you have to change table name. What is more flexible is to change table name in linq chain

context.DbSet<Goods>().TableName(“GOODS_” + year)
   .Where(..)
   ...

sdanyliv avatar May 19 '19 04:05 sdanyliv

@sdanyliv I think the suggested method to deal with that problem in relational databases is to use a separate column called year and then just include it in any queries you write i.e. SELECT * FROM goods WHERE year=1995.

Sure, you might have more rows in the table causing minor performance issues, but relational DBs are designed to handle a large number of rows.

shravan2x avatar May 20 '19 22:05 shravan2x

@shravan2x, it is not suggested method it is how database designed to avoid table locks. And it is just sample. In real situation there are more complicated cases.

sdanyliv avatar May 21 '19 05:05 sdanyliv

Oh that's interesting, I haven't encountered something like that before. Do you have any pointers to example use cases?

shravan2x avatar May 21 '19 06:05 shravan2x

@shravan2x I had a similar use-case with a legacy application in a previous job... The "primary key" (generated in a client app) was appended as a suffix to related table names. It was a workaround for slow data access times using VB6 + Microsoft Access. The challenge is developers want to use a modern tech stack that improves their productivity, but sometimes have to put patches in to support legacy systems... I wound up writing a small ORM very similar to Dapper to support accessing that system.

The use-case I would recommend implementing - and wouldn't mind doing - would allow a syntax similar to:

context.Dbset<T>.ToTable("Goods", null /* default */).WithPrefix($"{entity.DateField.Year}_");
// - AND ALSO -
context.DbSet<T>.ToTable("Goods", "Shipping").WithSuffix($"_{entity.PrimaryKey}");

This sort of use-case kind of stinks, but it's a real use-case for more than a few customer scenarios I'm sure. In most cases, I think the utility of a capability like this is based more on integrating with legacy applications than it is on new projects.

@sdanyliv, does the markdown above adequately capture what you were thinking?

The obvious trade-off is that implementing something like what I described effectively encourages continuing designs similar to this in non-legacy scenarios, where proper indexing and design would likely solve whatever problems this pattern is attempting to solve... How difficult would it be for a customer to support a use-case similar to this as an extension?

As a consumer, I'd rather use a product that's got a ton of good integration testing like EFCore than rolling my own implementation.

Do you guys have any ideas about how you'd like to see the original issues might be implemented that someone could review? I wouldn't mind taking a stab.

briandrennan avatar Dec 17 '19 07:12 briandrennan