pnolan
March 19th, 2009, 09:50 PM
Hi,
I have a question regarding the Entity Framework that I am hoping you could answer. It is a lengthy post but I think it is a very important question.
Background
I have defined a model with Table-per-Type Inheritance using the EF. For the purpose of this question let’s just say I have 2 tables, Person and Student. Of course I will also have two .net objects of type Person and Student. Student inherits from person and person is Not an abstract class.
Because the DB Schema is experiencing some fundamental changes in coming months, an additional layer of abstraction is required. For this reason the store:Type must equal ‘Views’ rather than ‘Tables’.
Because EF considers dbo.views to be read-only, I am forced to utilize sprocs or inline edmx functions to perform the CUD (Create|Update|Delete). As a side topic, it would be nice if EF supported read-write dbo.views.
Due to a number of reasons I have decided to use sprocs rather than inline functions. So I have created 6 sprocs dbo.Person_Insert, dbo.Person_Update, dbo.Person_Delete, dbo.Student_Insert, dbo.Student_Update, dbo.Student_Delete.
An example of the sproc interface for Person_Insert and Student_Insert is:
PROC [dbo].[Person_Insert]
@Id uniqueidentifier,
@FirstName nvarchar(50),
@LastName nvarchar(50),
@TimeStamp timestamp
AS
PROC [dbo].[Student_Insert]
@Id uniqueidentifier,
@Number nvarchar(50),
@TimeStamp timestamp
AS
Issue
As you can see, when I insert a Student object, I was expecting the EF would identify that Student inherits from Person which also has associated sproc functions. I would then expect the EF to call Person_Insert and then call Student_Insert. This Insert would have to be performed within an atomic transaction so that if Student_Insert fails it would rollback the Person record already inserted.
There would be many advantages to this approach, including:
1. More accurate representation of Table-per-Type Inheritance
2. Clear separation of concerns. CRUD specific to the Student object is separate. This is more closely aligned with Test-Driven Development.
3. Sprocs adhere more closely to ACID theory.
4. There is less duplicity of code within sprocs.
5. In my case sprocs will be closely aligned with views, therefore automatic CRUD generation can be more easily utilized.
However, what I have discovered is that when I insert a Student object, only the Student_Insert sproc is called. This means that the Student_Insert must look like
PROC [dbo].[Student_Insert]
@Id uniqueidentifier,
@FirstName nvarchar(50),
@LastName nvarchar(50),
@Number nvarchar(50),
@TimeStamp timestamp
AS
Question
Is there a way to easily configure my EF Model to call the sprocs associated with Person and Student when I am inserting, updating or deleting a Student object?
Thanks heaps,
Patrick
I have a question regarding the Entity Framework that I am hoping you could answer. It is a lengthy post but I think it is a very important question.
Background
I have defined a model with Table-per-Type Inheritance using the EF. For the purpose of this question let’s just say I have 2 tables, Person and Student. Of course I will also have two .net objects of type Person and Student. Student inherits from person and person is Not an abstract class.
Because the DB Schema is experiencing some fundamental changes in coming months, an additional layer of abstraction is required. For this reason the store:Type must equal ‘Views’ rather than ‘Tables’.
Because EF considers dbo.views to be read-only, I am forced to utilize sprocs or inline edmx functions to perform the CUD (Create|Update|Delete). As a side topic, it would be nice if EF supported read-write dbo.views.
Due to a number of reasons I have decided to use sprocs rather than inline functions. So I have created 6 sprocs dbo.Person_Insert, dbo.Person_Update, dbo.Person_Delete, dbo.Student_Insert, dbo.Student_Update, dbo.Student_Delete.
An example of the sproc interface for Person_Insert and Student_Insert is:
PROC [dbo].[Person_Insert]
@Id uniqueidentifier,
@FirstName nvarchar(50),
@LastName nvarchar(50),
@TimeStamp timestamp
AS
PROC [dbo].[Student_Insert]
@Id uniqueidentifier,
@Number nvarchar(50),
@TimeStamp timestamp
AS
Issue
As you can see, when I insert a Student object, I was expecting the EF would identify that Student inherits from Person which also has associated sproc functions. I would then expect the EF to call Person_Insert and then call Student_Insert. This Insert would have to be performed within an atomic transaction so that if Student_Insert fails it would rollback the Person record already inserted.
There would be many advantages to this approach, including:
1. More accurate representation of Table-per-Type Inheritance
2. Clear separation of concerns. CRUD specific to the Student object is separate. This is more closely aligned with Test-Driven Development.
3. Sprocs adhere more closely to ACID theory.
4. There is less duplicity of code within sprocs.
5. In my case sprocs will be closely aligned with views, therefore automatic CRUD generation can be more easily utilized.
However, what I have discovered is that when I insert a Student object, only the Student_Insert sproc is called. This means that the Student_Insert must look like
PROC [dbo].[Student_Insert]
@Id uniqueidentifier,
@FirstName nvarchar(50),
@LastName nvarchar(50),
@Number nvarchar(50),
@TimeStamp timestamp
AS
Question
Is there a way to easily configure my EF Model to call the sprocs associated with Person and Student when I am inserting, updating or deleting a Student object?
Thanks heaps,
Patrick