I know that in schema less mode property names have to be saved along property values, therefore the shorter name the better.
How does it work with partial schema? Are only schemaless properties saved with property names?
What is default schema type? Do I have to remember to set strictmode after each class declaration to save space?
Related
My MS SQL 2014 database table has a computed property column which uses a database function. Using SQL Server Management Studio, a query against the table lists the computed property values as expected.
The Codefluent model created via the import wizard shows the Entity with the computed column as a property. The underlying .cpf file defines the property with "d3p1:compute=" and the list of parameters that are used by the database function.
When an entity or the collection of entities is loaded, the properties which are used in the computed property have values, yet the computed property has a value of nothing/null.
How do I get Codefluent to read the computed value from the database table and have the value included in the entity's properties?
This is a bit tricky. First of all, you should declare the property like any other property. Then you must instruct the SQL producer to declare a formula on that column. You can do that with a custom 'compute' attribute in the SQL producer namespace. You can set it with the Visual Studio modeler like this:
In this example I've created an int property that is just another column value multiplied by 2.
Optionally, you can declare the property to be 'read on save' because most of the time, you want to read the computed value after a save, not only on load operations:
Once this is all done, this sample console app should display 30:
class Program
{
static void Main(string[] args)
{
var c = new Customer();
c.Name = "killroy";
c.Age = 15;
c.Save();
Console.WriteLine(c.Age2); // will display 30
}
}
If Simon Mouriers solution solves your problem than that is probably the best approach. However, there are 2 other options
RAW View Method
After you create a Codefluent Entities View click on the Edit Where button and it will allow you to create a RAW View
You can than specify the advanced property "UsedForMethods".
WARNING: Related entities will use the table instead of the view. This is by design and there is an article somewhere on the knowledge center on how to get around it. http://www.softfluent.com/product/codefluent-entities/knowledge-center/
Rename SQL Tables and Create a SQL View with the Same Name as the Original Table - This method is a hack, Softfluent discourages this approach, I love it because I know exactly what is happening under the scenes. I have used it with success in a scenario in which I needed soft deletes. I have automated the process with 2 stored procedures that handle the renaming. Using this approach requires running one of the stored procedures to undo the name changing prior to building the model. The other stored procedure handles the renaming after building the model. I'll post the stored procedures and how I use them within a couple of days.
I have a MongoEngine Document that previously didn't allow inheritance. I then wanted to inherit from it, so I set {allow_inheritance:True}. As soon as I did that the existing documents for that model didn't appear anymore when calling <myModel>.objects. If I momentarily set {allow_inheritance:False} then the documents come back. Why would that be?
I'm using MongoEngine verison 0.8.7
Figured it out. When using allow_inheritance, MongoEngine stores a special _cls field in the base document with the name of the class, or derived class. So for your BaseClass it would store "BaseClass" as the value, and for your DerivedClass it would would store "BaseClass.DerivedClass" as the value. But without allow_inheritance set initially, it does not have this special _cls field set. So after setting allow_inheritance I had to go in to the mongo field manually (not through mongoengine) and perform an update to add the _cls field with the BaseClass value and then documents reappeared.
I'm using EF5 and Code-First.
I have an abstract base class called FooBase. Foo1 and Foo2 both inherit from FooBase. I use EF's mapping configuration based on a Discriminator called Bar. So if Bar = someValue, create Foo1... if Bar is anotherValue, create Foo2.
Since Bar is a discriminator, I don't have access to it as a property on any of the Foo classes.
I now have the need to convert an instance of Foo1 to Foo2. I use reflection and move the properites to the new instance, including Foo1's key value. It's an exact duplicate, except that it is a different type. I change the EntityState to modified and save to the database.
However, the discriminator value in the database is not getting updated. It still remains the same value as if it was still Foo1.
I can guess that since the enitity is only set to modified, EF doesn't bother checking the discriminator.
Does anyone know a way around this?
You cannot change existing instance to another type. Type of the entity associated with key value is immutable. If you want to create Foo2 from Foo1 you need to create a new instance with a new key value and insert it to database. Just modifying will always keep the key and discriminator.
If you seriously need to change the type and keep the key inheritance is not solution for you. Think about the key as equivalent to reference and about discriminator as equivalent to type in .NET - you cannot change type of existing reference.
Given an instance of EdmType, how do I get the database schema of the corresponding table?
I could find a property for the table name (of course), but nothing for the schema.
I looked at the properties and meta data. Nothing.
Am I missing something?
EdmType doesn't contain such information. You need instance of EntitySet from System.Data.Metadata.Edm namespace and search its MetadataProperties collection for MetadataItem with Name == "Schema". The Value property of this item will contain database schema.
I'm working on a custom entity framework provider and I need to add support for default column values for this provider. When the user uses the entity framework wizard and selects a table that includes columns with default values, those default values are not being populated into the entity designer.
I'm a little lost on where exactly this population should take place. I believe the appropriate place would be in the GetEdmType method override of DbXmlEnabledProviderManifest but I just don't see how to set the default value, if this is the correct place.
Anybody has experience writing EF providers that support default values for table columns? How do you implement this?
I am a bit late to the party but DbXmlEnabledProviderManifest is not the right place for adding default values. The provider manifest describes capabilities of the database engine itself and is specific (and general) to this database engine and not to a given database and/or table. The default value in the provider manifest tells EF what value to use for the given column property if one is not provided by the user (e.g. if the user user does not specify scale or precision for a decimal column the value from provider manifest will be used for scale and/or precision used for this column).
If you want just to insert a default value for a property the easiest way is to set the property that corresponds to the column on your entity to this value in the constructor. This way the user can always set it to a different value but if s/he does not the default value will be sent to the database. For some corner case scenarios where some of the columns in the database do not have corresponding properties on entities you can use DefaultValue attribute on the Property element in SSDL which will be inserted to the database when you add a row. This is especially useful if those properties are not nullable since without telling EF what value should be inserted EF would try inserting null which would obviously fail for non-nullable columns.