EF6 Table-per-Hierarchy (TPH) - abstract base class with abstract property does not generate a migration
I have an abstract base class set up for TPH:
public abstract class BaseSchema
{
...
public abstract bool IsFilterRequired { get; set; }
...
}
.. and I inherit from it in other derived classes as per:
public class DerivedSchema : BaseSchema
{
.....
public override bool IsFilterRequired{ get; set; }
....
}
This setup works fine for the other inherited properties in these classes (those i have left out) and this is working to date.
It's the addition of the 'IsFilterRequired' property that i've shown above that's the issue - I'm adding this abstract property to the base class and overriding it in the derived class. I would expect that EF would pick this up and generate a migration to add the new column to the SQL Server table, but it just creates an empty migration....
What could be wrong with this?
Thanks!
c# entity-framework-6 tph
add a comment |
I have an abstract base class set up for TPH:
public abstract class BaseSchema
{
...
public abstract bool IsFilterRequired { get; set; }
...
}
.. and I inherit from it in other derived classes as per:
public class DerivedSchema : BaseSchema
{
.....
public override bool IsFilterRequired{ get; set; }
....
}
This setup works fine for the other inherited properties in these classes (those i have left out) and this is working to date.
It's the addition of the 'IsFilterRequired' property that i've shown above that's the issue - I'm adding this abstract property to the base class and overriding it in the derived class. I would expect that EF would pick this up and generate a migration to add the new column to the SQL Server table, but it just creates an empty migration....
What could be wrong with this?
Thanks!
c# entity-framework-6 tph
1
The problem is of course the abstract property. EF doesn't like such things. It maps only "physical" properties.
– Ivan Stoev
Nov 22 '18 at 17:13
1
Yes, that's the answer :) Making this property virtual allows it to be picked up by an EF migration. What caught me out was that EF does create the Sql table based on the abstract base class in TPH, so i assumed abstract properties were fine as well. Thanks Ivan. Can you change your comment to an answer so i can mark it as the solution?
– Ciaran
Nov 23 '18 at 9:36
Hi @Ciaran, You are welcome and glad of being helpful :) But I didn't go too deep into it and can't find (no time to search) for some documentation, so I think it would be more appropriate if you post your own self answer.
– Ivan Stoev
Nov 23 '18 at 10:56
add a comment |
I have an abstract base class set up for TPH:
public abstract class BaseSchema
{
...
public abstract bool IsFilterRequired { get; set; }
...
}
.. and I inherit from it in other derived classes as per:
public class DerivedSchema : BaseSchema
{
.....
public override bool IsFilterRequired{ get; set; }
....
}
This setup works fine for the other inherited properties in these classes (those i have left out) and this is working to date.
It's the addition of the 'IsFilterRequired' property that i've shown above that's the issue - I'm adding this abstract property to the base class and overriding it in the derived class. I would expect that EF would pick this up and generate a migration to add the new column to the SQL Server table, but it just creates an empty migration....
What could be wrong with this?
Thanks!
c# entity-framework-6 tph
I have an abstract base class set up for TPH:
public abstract class BaseSchema
{
...
public abstract bool IsFilterRequired { get; set; }
...
}
.. and I inherit from it in other derived classes as per:
public class DerivedSchema : BaseSchema
{
.....
public override bool IsFilterRequired{ get; set; }
....
}
This setup works fine for the other inherited properties in these classes (those i have left out) and this is working to date.
It's the addition of the 'IsFilterRequired' property that i've shown above that's the issue - I'm adding this abstract property to the base class and overriding it in the derived class. I would expect that EF would pick this up and generate a migration to add the new column to the SQL Server table, but it just creates an empty migration....
What could be wrong with this?
Thanks!
c# entity-framework-6 tph
c# entity-framework-6 tph
asked Nov 22 '18 at 17:06
CiaranCiaran
12918
12918
1
The problem is of course the abstract property. EF doesn't like such things. It maps only "physical" properties.
– Ivan Stoev
Nov 22 '18 at 17:13
1
Yes, that's the answer :) Making this property virtual allows it to be picked up by an EF migration. What caught me out was that EF does create the Sql table based on the abstract base class in TPH, so i assumed abstract properties were fine as well. Thanks Ivan. Can you change your comment to an answer so i can mark it as the solution?
– Ciaran
Nov 23 '18 at 9:36
Hi @Ciaran, You are welcome and glad of being helpful :) But I didn't go too deep into it and can't find (no time to search) for some documentation, so I think it would be more appropriate if you post your own self answer.
– Ivan Stoev
Nov 23 '18 at 10:56
add a comment |
1
The problem is of course the abstract property. EF doesn't like such things. It maps only "physical" properties.
– Ivan Stoev
Nov 22 '18 at 17:13
1
Yes, that's the answer :) Making this property virtual allows it to be picked up by an EF migration. What caught me out was that EF does create the Sql table based on the abstract base class in TPH, so i assumed abstract properties were fine as well. Thanks Ivan. Can you change your comment to an answer so i can mark it as the solution?
– Ciaran
Nov 23 '18 at 9:36
Hi @Ciaran, You are welcome and glad of being helpful :) But I didn't go too deep into it and can't find (no time to search) for some documentation, so I think it would be more appropriate if you post your own self answer.
– Ivan Stoev
Nov 23 '18 at 10:56
1
1
The problem is of course the abstract property. EF doesn't like such things. It maps only "physical" properties.
– Ivan Stoev
Nov 22 '18 at 17:13
The problem is of course the abstract property. EF doesn't like such things. It maps only "physical" properties.
– Ivan Stoev
Nov 22 '18 at 17:13
1
1
Yes, that's the answer :) Making this property virtual allows it to be picked up by an EF migration. What caught me out was that EF does create the Sql table based on the abstract base class in TPH, so i assumed abstract properties were fine as well. Thanks Ivan. Can you change your comment to an answer so i can mark it as the solution?
– Ciaran
Nov 23 '18 at 9:36
Yes, that's the answer :) Making this property virtual allows it to be picked up by an EF migration. What caught me out was that EF does create the Sql table based on the abstract base class in TPH, so i assumed abstract properties were fine as well. Thanks Ivan. Can you change your comment to an answer so i can mark it as the solution?
– Ciaran
Nov 23 '18 at 9:36
Hi @Ciaran, You are welcome and glad of being helpful :) But I didn't go too deep into it and can't find (no time to search) for some documentation, so I think it would be more appropriate if you post your own self answer.
– Ivan Stoev
Nov 23 '18 at 10:56
Hi @Ciaran, You are welcome and glad of being helpful :) But I didn't go too deep into it and can't find (no time to search) for some documentation, so I think it would be more appropriate if you post your own self answer.
– Ivan Stoev
Nov 23 '18 at 10:56
add a comment |
1 Answer
1
active
oldest
votes
Thanks to @ivan-stoev for the direction. Basically, abstract properties are ignored by EF. If you wish to specify a property on your base class to be inherited by derived classes, you will want EF to create a database column to represent this. Marking the property as 'virtual' allows EF to create the column:
public abstract class BaseSchema
{
...
public virtual bool IsFilterRequired { get; set; }
...
}
As an aside, if you have multiple derived classes inheriting from the common TPH base, this property will now be available to all of them. If this is behavior you don't want, you'll need to override it in the derived classes it doesn't belong to and mark it as not implemented. Not ideal, but better than the alternative and it'll throw a very easy to catch runtime error that'll get caught in Unit Testing.
public class DerivedSchemaThatDoesNotNeedThisProperty : BaseSchema
{
...
public override bool IsFilterRequired
{
get { return false; }
set { throw new NotImplementedException($"{nameof(IsFilterRequired)} property is not implemented in this class."); }
}
...
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435590%2fef6-table-per-hierarchy-tph-abstract-base-class-with-abstract-property-does%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks to @ivan-stoev for the direction. Basically, abstract properties are ignored by EF. If you wish to specify a property on your base class to be inherited by derived classes, you will want EF to create a database column to represent this. Marking the property as 'virtual' allows EF to create the column:
public abstract class BaseSchema
{
...
public virtual bool IsFilterRequired { get; set; }
...
}
As an aside, if you have multiple derived classes inheriting from the common TPH base, this property will now be available to all of them. If this is behavior you don't want, you'll need to override it in the derived classes it doesn't belong to and mark it as not implemented. Not ideal, but better than the alternative and it'll throw a very easy to catch runtime error that'll get caught in Unit Testing.
public class DerivedSchemaThatDoesNotNeedThisProperty : BaseSchema
{
...
public override bool IsFilterRequired
{
get { return false; }
set { throw new NotImplementedException($"{nameof(IsFilterRequired)} property is not implemented in this class."); }
}
...
}
add a comment |
Thanks to @ivan-stoev for the direction. Basically, abstract properties are ignored by EF. If you wish to specify a property on your base class to be inherited by derived classes, you will want EF to create a database column to represent this. Marking the property as 'virtual' allows EF to create the column:
public abstract class BaseSchema
{
...
public virtual bool IsFilterRequired { get; set; }
...
}
As an aside, if you have multiple derived classes inheriting from the common TPH base, this property will now be available to all of them. If this is behavior you don't want, you'll need to override it in the derived classes it doesn't belong to and mark it as not implemented. Not ideal, but better than the alternative and it'll throw a very easy to catch runtime error that'll get caught in Unit Testing.
public class DerivedSchemaThatDoesNotNeedThisProperty : BaseSchema
{
...
public override bool IsFilterRequired
{
get { return false; }
set { throw new NotImplementedException($"{nameof(IsFilterRequired)} property is not implemented in this class."); }
}
...
}
add a comment |
Thanks to @ivan-stoev for the direction. Basically, abstract properties are ignored by EF. If you wish to specify a property on your base class to be inherited by derived classes, you will want EF to create a database column to represent this. Marking the property as 'virtual' allows EF to create the column:
public abstract class BaseSchema
{
...
public virtual bool IsFilterRequired { get; set; }
...
}
As an aside, if you have multiple derived classes inheriting from the common TPH base, this property will now be available to all of them. If this is behavior you don't want, you'll need to override it in the derived classes it doesn't belong to and mark it as not implemented. Not ideal, but better than the alternative and it'll throw a very easy to catch runtime error that'll get caught in Unit Testing.
public class DerivedSchemaThatDoesNotNeedThisProperty : BaseSchema
{
...
public override bool IsFilterRequired
{
get { return false; }
set { throw new NotImplementedException($"{nameof(IsFilterRequired)} property is not implemented in this class."); }
}
...
}
Thanks to @ivan-stoev for the direction. Basically, abstract properties are ignored by EF. If you wish to specify a property on your base class to be inherited by derived classes, you will want EF to create a database column to represent this. Marking the property as 'virtual' allows EF to create the column:
public abstract class BaseSchema
{
...
public virtual bool IsFilterRequired { get; set; }
...
}
As an aside, if you have multiple derived classes inheriting from the common TPH base, this property will now be available to all of them. If this is behavior you don't want, you'll need to override it in the derived classes it doesn't belong to and mark it as not implemented. Not ideal, but better than the alternative and it'll throw a very easy to catch runtime error that'll get caught in Unit Testing.
public class DerivedSchemaThatDoesNotNeedThisProperty : BaseSchema
{
...
public override bool IsFilterRequired
{
get { return false; }
set { throw new NotImplementedException($"{nameof(IsFilterRequired)} property is not implemented in this class."); }
}
...
}
answered Nov 23 '18 at 11:16
CiaranCiaran
12918
12918
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435590%2fef6-table-per-hierarchy-tph-abstract-base-class-with-abstract-property-does%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
The problem is of course the abstract property. EF doesn't like such things. It maps only "physical" properties.
– Ivan Stoev
Nov 22 '18 at 17:13
1
Yes, that's the answer :) Making this property virtual allows it to be picked up by an EF migration. What caught me out was that EF does create the Sql table based on the abstract base class in TPH, so i assumed abstract properties were fine as well. Thanks Ivan. Can you change your comment to an answer so i can mark it as the solution?
– Ciaran
Nov 23 '18 at 9:36
Hi @Ciaran, You are welcome and glad of being helpful :) But I didn't go too deep into it and can't find (no time to search) for some documentation, so I think it would be more appropriate if you post your own self answer.
– Ivan Stoev
Nov 23 '18 at 10:56