Why when using TPH inheritance are default values ignored? - entity-framework-4

CREATE TABLE Lodging
(
LodgingID int PRIMARY KEY IDENTITY,
LodgingName nvarchar(100),
Resort bit NOT NULL,
ResortChainOwner nvarchar(100) NOT NULL,
...
)
Lodging DB table maps to Lodging entity. I've then implemented a TPH inheritance:
• With Lodging entity being the base
• I've created a new entity Resort, which derives from Lodging.
• I moved resort-specific property ResortChainOwner to the Resort entity type and also set ( in SSDL ) ResortChainOwner's Default Value to "notApplicable"
• Lodging table's Resort column is a discriminatory column. If Resort column contains 0, then records is materialized into Lodging entity and if column contains 1, then record is materialized into Resort entity
When in the following code SaveChanges is called, EF should supply (to Insert command) a default value for ResortChainOwner field, but instead it supplies a Null
var lodging = new Lodging();
...
context.Lodgings.AddObject(lodging);
context.SaveChanges();
So how do I on inserts force EF to supply a default value for ResortChainOwner column?
Thank you

You've defined a default value for the Resort class, but you've instantiated a the super class "Lodging". So instead use:
var lodging = new Resort();
The base class "Lodging" doesn't even have that property anymore if I understand you well.

Try setting your Lodging entity to abstract and create a second entity which derives from Lodging. I have a feeling TPH works only on classes actually inheriting from the class containing the discriminator field.

Related

Is there a way to make Entity Framework Core map all Guid properties to nvarchar without annotations?

I have a domain model that makes use of Guids for primary/foreign keys. Is there a way to instruct the ModelBuilder to explicitly create database tables with nvarchar storage for these properties without applying annotations on every existing primary and foreign key in my entity classes?
Use the Microsoft.EntityFrameworkCore.Storage.ValueConversion.GuidToStringConverter value converter.
var guidToStringConverter = new GuidToStringConverter();
modelBuilder.Entity<YourEntity>()
.Property(ye => ye.PropertyThatIsAGuid)
.HasConversion(guidToStringConverter);

Entity Framework and SQL Server table properties

We have a SQL Server 2008 R2 database with several tables and each table has a number of triggers. On one of the columns, we'll call this Person.Age we have a default value, so that if I don't explicitly supply a value it defaults to "18".
create table PERSON
(
id int IDENTITY(1,1) PRIMARY KEY,
age char(2) DEFAULT '18',
Name char(40),
);
I am using EntityFramework 4.0 (and have also tried 5.0) and Visual Studio 2010, to load and select from the database. Whenever I insert into the table using the following statement, it is inserting a row, but it isn't completing the default value:
var person = new Person
{
Name = "Peter"
};
using (var ctx = new MyEntities())
{
ctx.PERSON.AddObject(person);
ctx.SaveChanges();
}
This will result in a row with a Name of Peter, but the Age will be set to null - and not my default of 18.
When I refresh/load my EDMX file I can only seem to import simple tables and views and there doesn't appear to be an option for importing the properties - although I would have thought this was done by default? Any ideas why the default properties aren't firing?
Also, I have triggers defined in SQL Server so that when a new row is inserted into PERSON, an additional table gets updated. Again this works if I run the SQL direct against the database, but doesn't work if I execute through Visual Studio using EntityFramework.
Thanks,
EF will explicitly set the columns to values you passed. Since, when you created a Person entity, the value for age will be by default set to null EF will send a command in which it will set the column value to null. Set the default value in the ctor if you want to have the default value (otherwise the default value is null for reference properties and default(T) for value type properties (e.g. int)).
The EF designer brings all the columns from the database and create a model with entities that have properties coresponding to the values to the tables and columns it reversed engieneered. You can then go and tweak your model in the designer - for instance you can remove properties you don't want.
I don't know what "does not work" means for you in case of the triggers - it probably depends on your expectation. EF just sends a command to the database. So if you send the same Sql command as the EF sends it should "not work" in the same way. Having said that EF is database agnostic and is not aware of DB magic like triggers. Also the communication is one way only from EF to the DB. So, if you expect that the database notifies the EF about something then it will not work. There are no means for doing this.

Do Partially Defined Entiy Classes in code-first approach work?

I am using the code-first approach in Entity Framework 4.0. I have defined an entity class called 'Product' which contains these properties only - ProductId, ProductName and ProductDescription.
However, the table in database called 'Products' has additional columns like ProductAge, ProductWeight, ProductVolume and IsActive. Will updating/inserting using Entity Framework code-first approach still work with a partially defined entity class?
Trivial issue would be inserts to the table if the unmapped columns are not nullable. Otherwise inserts will work fine. Assuming you are not going to recreate the database from your code first model, it should work.

Object Generated From Entity Model Does Not Create Nullable Foreign Keys

I have noticed some strange behavior when it comes to nullable foreign keys in my database. I have just started playing with the entity framework so I'm probably doing something wrong, but I cannot figure this one out.
Say I have the following two tables: (CountryID is a foreign key that is nullable)
When I create a new entity model I end up with this:
But the CountryID properties are set to Nullable (None) - instead of True. Obviously this is a really trivial example, but with a large database it would be difficult switch all of these manually. Is there any way to have the entity framework use a nullable int? for these foreign keys?

Rails ActiveRecord Question

Single Table Inheritance using ActiveRecord. Since we can use #test = Employee.all and find all the employees created. How does rails do this? Since we only use a User Table. How does it know about employees and retrieve only employees? Rails Magic? Explanation anyone? Thank you in advance.
Base Class : Person (inherits ActiveRecord)
Sub-Class: Employee, Supervisor, Manager (each inherit Person)
So my Person table needs to have a _type and _id field to make the table polymorphic.
My next question is how do I get Employee Associated to the Person table and when you save an employee, how do you get it to actually put in Employee in the person_type field?
To indicate to Ruby on Rails that the
users table needs to support Single
Table Inheritance you need to add a
column named ‘type’ to the users
table. Here is my users table
definition:
CREATE TABLE users ( id INT NOT
NULL AUTO_INCREMENT, user
VARCHAR(15) NOT NULL UNIQUE, pass
VARCHAR(40) NOT NULL, type
VARCHAR(20) NOT NULL, PRIMARY KEY (id) );
In the column named type you
should store the name of the class,
the class type, that should be used
for each user. To mark an certain user
as an admin set his type to
‘Administrator’. By setting a user’s
type to ‘Administrator’ you are giving
him full administrator privileges as
defined in your Administrator model
class.
http://juixe.com/techknow/index.php/2006/06/03/rails-single-table-inheritance/
Single table inheritance uses a type column on the table to indicate the type of the object. ActiveRecord knows that your Employee class is using single table inheritance (it has no matching table and the users/people table has a type column).
So when you ask for Employee.all it knows to looks for all entries in the users/people table where type == 'Employee'.
If you look at the logs the SQL will be displayed for these queries and you'll see the 'magic' happening.

Resources