I am using MVC4 with EF5 database first and Newtonsoft.Json for serializing objects to JSON for example in Web API controllers.
To avoid the problem of self referencing loops i set the attribute [JsonIgnore] to the specific collections in my generated classes.
My problem is now that each time i update my model i have to readd the attributes to the classes.
How can i avoid that? I think i have to edit the DBModel.tt script? What have i to do?
You should be able to use a metadata class with the MetadataType attribute. If your generated class is:
public partial class MyClass{
public string SomeProperty {get; set; }
public string SomePropertyToIgnore {get; set; }
}
Then you need to create a metadata class like so (in the same namespace):
public class MyClass_Metadata{
[JsonIgnore]
public string SomePropertyToIgnore {get; set; }
}
The create a partial of your generated class (in the same namespace) with the MetadatType attribute applied:
[MetadataType(typeof(MyClass_Metadata))]
public partial class MyClass{
}
Ref: http://msdn.microsoft.com/en-us/library/ee707339(v=vs.91).aspx
Related
My project (Xamarin.Forms with FreshMVVM) contains multiple pages and I need property isBusy. My PageModels inherit from FreshBasePageModel class. I'm looking for a way to extend FreshBasePageModel class to add IsBusy property.
Is there a way to do this? Multiple inheritance is not allowed in C#.
Using extension methods i only add methods (not properties).
There is an idea to add a new class (FreshBasePageModelExt) that inherits from the FreshBasePageModel class, and use this new class as a base class for my PageModels, but perhaps there is a more elegant solution.
Mine looks like this
public class BaseViewModel : FreshBasePageModel
{
public bool IsBusy { get; set; }
public string Title { get; set; }
}
By creating this BaseViewModel and inheriting your viewModels from this will give you these attributes and then you have them in all your ViewModels.
I am using the SQLite.Net-PCL library to manage the SQLite database in my UWP app. The documentation says that calling CreateTableAsync is able to add columns to the table if a properly is added to the data model. However, when I add a property, the application throws an exception that says the table does not have a column named . This means that the new column was not created automatically. I am calling CreateTableAsync in the constructor of the class that manages database calls for the table with a repository design pattern.
I think you missed decorations [PrimaryKey, AutoIncrement] for your class model. Please check the following DataTemple class.
public class DataTemple
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; } //primary key
public string Name { get; set; }
public string Age { get; set; }
}
For more detail you could also refer this sample.
I pulled out all of my CreateTableAsync calls from the repository class constructors and moved them into separate Init Tasks so I can properly await. Now I can add columns to any table without problems.
I have ignored a column through fluent API but want to populate that property while executing stored procedure using some logic. But it is not mapping the ignored column property. Please let me know if there is any way to do this in Entity framework code first.
I've faced with the same problem recently. The only solution I found is a class hierarchy:
public class MyEntityBase {
public int Id { get; set; }
}
public class MyEntity: MyEntityBase {
...
}//This class is mapped to DB with a fluent API and does not contain ignored property.
//Also it does not have derivative classes, so EF will not create class inheritance in DB.
public class DerivedEntity: MyEntityBase {
public int IgnoredProperty { get; set; }
}//Use this class while executing stored procedures
P.S. Do not mark class MyEntityBase as ABSTRACT - EF will map this relationship as database inheritance.
I noticed that when serializing/deserializing a POCO class with JSON.NET that derives from a base class that uses explicit DataContract attributes then any POCO properties on the derived class are ignored.
For example, in this example, "Address" is not serialized/deserialized:
[DataContract]
public class MyBaseClass
{
[DataMember(Name = "SomeName")]
public string Name { get; set; }
}
public class MyDerivedClass : MyBaseClass
{
public string Address { get; set; }
}
Is that intentional?
FWIW, it does seem that DataContractSerializer (at least the XML one) does the "right thing" here and serializes/deserializes "Address".
Thanks!
Henrik
In JSON.NET, at least as of v4.5.6, it detects the DataContract of the base class and assumes opt-in serialization. Since Address is not decorated with the DataMember attribute it does not get serialized.
This is by design according to James Newton-King with no planned change.
I've a problem when adding metadata to a class named as a reserved keyword. During the generation of the data model (the DBML file), an # has been added to the class name to have it working.
When I apply metadata to the class, it is not working (metadata info is not taken in consideration - the DisplayName and other validation stuff). For all other classes of my project (that do not have a class name as a reserved keyword, it is working). It is currently not possible to rename that class.
Generated class definition:
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.operators")]
public partial class #operator : INotifyPropertyChanging, INotifyPropertyChanged
{...}
MetaData class definition:
[MetadataType(typeof(OperatorMetaData))]
public partial class #operator
{
}
public class OperatorMetaData
{
[DisplayName("Operator Type")]
[Required(ErrorMessage = "Operator type is required.")]
public int operator_type_id { get; set; }
...
}
Anyone any idea how to work around this? Or what did I do wrong ?