-3

How can I interchangeably use entity base Id or database column Id

public partial class Orders : EntityBase<int>
{
    protected Orders() { }
    public int Orderid { get; set; }
    public override int Id { get => base.Id; set => base.Id = Orderid; }
}

Entitybase:

public abstract class EntityBase<T> : IEntity<T>
{
    public virtual T Id { get; set; }
}

Question: Can I map this Id of EntityBase and db column's primary key(for eg: order entity's key -> orderId) to sync values ( In app code, either user set Id of base or orderId of entity both should contain same value and also when retrieved also these values, at a given time, should return same value. Is there any way to achieve above synch feature via fluent API in the OnModelCreating() method? P.S: If you have not understood the question, please say which part does not have clarification, instead of using authority :)

HydTechie
  • 797
  • 10
  • 17
  • 2
    I don't understand what you are asking at all? – DavidG Jun 17 '18 at 19:57
  • @DavidG - hope the recent edit will clarify the question for you. – HydTechie Jun 20 '18 at 07:39
  • Sorry, nope, it did not. – quetzalcoatl Jul 06 '18 at 07:27
  • After reading the title, I jumped in and was going to write "hey, ID may be not enough depending on the db design. If you mapped derived types to the same table, it's all ok as they all use the same PK, but if you mapped derived types to separate tables, then the database could have generated the same IDs for many different objects of different type. In that case, you may need to add some 'discriminator' value to the ID, like "person:5" so you later know which table the ID was from". But, after reading your question, and after reading your edit, I have truly NO IDEA what you are asking about. – quetzalcoatl Jul 06 '18 at 07:31
  • Just add corresponding `[Key]` and `[NotMapped]` attributes. You will not be able to use not mapped property in queries. – kemsky Jul 07 '18 at 15:23

2 Answers2

1

Basically If I understand your question correctly, you want to have a base class with an Id, but in SQL you've named your Id Column differently. You want to Alias your updates so that when you set they Id, they also set the DB Id.

This is more of an architecture, thing, if you really needed to you could write tool that helps you scaffold automatically, but that seems buggy and more complex.

Instead I'll try to fix this problem by modifying your architecture. A simple way to fix this problem, is just to work with Id's

public partial class Orders : EntityBase<int>
{
    protected Orders() { }
    [Column("Orderid")]
    public int Id { get; set; }
}

You can map a column directly to the Id property, and avoid the whole issue all together. If you're set on having both OrderId and Id

public partial class Orders : EntityBase<int>
{
    protected Orders() { }
    public int Orderid { get; set; }
    public override int Id { get => Orderid; set => Orderid = value; }
}

but using that architecure is generally a bad idea, It's messy since your EntityBase Is Generic an seems error prone.

johnny 5
  • 19,893
  • 50
  • 121
  • 195
0

If i understand right you need that your Orders.Orderid is an autoincremental-PrimaryKey for your DB table right? Supposing you're using SQLite (if not probably is similar but not the same words) you can do something like that:

public partial class Orders : EntityBase<int> {

    [PrimaryKey, AutoIncrement]
    public int Orderid { get; set; }
    protected Orders() { }
    public override int Id { get => base.Id; set => base.Id = Orderid; } 
}

When you create/update your table using the orders "model" SQLite should do the rest. L-

Legion
  • 760
  • 6
  • 23