I have n-tier application that has many layers in different assemblies.
i use entity framework 6.1 and i want to add ObjectState
property to the base entity to track entity states. The problem is BaseEntity
is located in my domain objects dll that is database independent and i want to add ObjectState
in the Entity Framework project as this property is entity framework related.How to achieve this behavior?
public enum ObjectState
{
Unchanged,
Added,
Modified,
Deleted
}
public interface IObjectState
{
[NotMapped]
ObjectState ObjectState { get; set; }
}
You can use the partial classes, if you can edit the code of your domain objects projects and declare the base entity as partial class.
namespace DomainNameSpace
{
public partial class BaseEntity
{
// Your properties and method
}
}
Then in your Entity Framework project you can add the following code:
namespace DomainNameSpace
{
public partial class BaseEntity
{
public enum ObjectState
{
Unchanged,
Added,
Modified,
Deleted
}
public interface IObjectState
{
[NotMapped]
ObjectState ObjectState { get; set; }
}
}
}
Or if you can't edit the files in domain project or don't like this approach, maybe inheritance can help. In your Entity Framework project create a class like the following.
namespace YourProjectNameSpace
{
public class StatefulEntityClassName : BaseEntity
{
public enum ObjectState
{
Unchanged,
Added,
Modified,
Deleted
}
public interface IObjectState
{
[NotMapped]
ObjectState ObjectState { get; set; }
}
}
}
Hope this help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With