Base class/Entity in EntityFramework 5.0 - c#

I'm using Entity Framework 5 in Database First approach and I am using edmx file.
Most of my entities have 6 common fields. Fields like CreatedAt, CreatedBy etc. Now, I implemented some functions as extensions that can only be applied to IQueryable of those Entities that have the common fields. But when I implement the extension method, it can be accessed by any type of IQueryable as it's typed T and I can only define that the type T should always be of one type.
So, I thought I can give a base class to the entities which has common fields and define type T as that base type. But, it seems I can't do this.
Any idea on how to solve this or implement what I have explained above?

Don't create a base class. Create an Interface, like below:
public interface IMyEntity
{
DateTime CreatedAt { get; set; }
string CreatedBy { get; set; }
// Other properties shared by your entities...
}
Then, your Models will be like this:
[MetadataType(typeof(MyModelMetadata))]
public partial class MyModel : IMyEntity
{
[Bind()]
public class MyModelMetadata
{
[Required]
public object MyProperty { get; set; }
[Required]
public string CreatedBy { get; set; }
}
}

I'm a fan of:
public interface IShared
{
DateTime CreatedOn { get; set; }
}
public interface ISharedValidation
{
[Required]
DateTime CreatedOn { get; set; }
}
public interface IMyEntity: IShared
{
// Entity Specifics
string Username { get; set; }
}
public interface IMyEntityValidation: ISharedValidation
{
[Required]
string Username { get; set; }
}
Then, your Models will be like this:
[MetadataType(typeof(IMyEntityValidation))]
public partial class MyModel : IMyEntity
{
public object CreatedOn { get; set; }
public string Username { get; set; }
}
If T4 generated by Entity Framework then your non-autogenerated class would look like:
[MetadataType(typeof(IMyEntityValidation))]
public partial class MyModel : IMyEntity
{
}
Typically, it is not recommended to use Bind in Asp.Net MVC.

Related

Using Entity Framework Code-First, can I make my class generic and derive a store-able string field from the type?

My use-case:
I'd like to store a representation of a file tree in my local (SQLite) database using EF.
My model will be a simplified copy a much larger model on a remote SQL database (also in EF)
I'd like to use one, generic entity that self-refers to create a tree structure, and derives its 'type' field from one of the original entity types (FiletypeA, FiletypeB, Folder etc.. using the interface IFileSynchronisable)
I figured the best way was to make the class generic, and deriving a string field from the type using nameof(T) and Type.GetType("FiletypeA"), but I've got stuck trying to instantiate the class when building the model:
public class FileSyncObject<T> where T : class, IFileSynchronisable
{
[Key]
public Guid Id { get; set; }
public long ObjectId { get; set; }
//Can I derive T from some 'ObjectType' field in the record?
public string ObjectType { get { return nameof(T); } }
public long ProjectId { get; set; }
public string AmazonS3Path { get; set; }
public bool IsActive { get; set; }
public string Version { get; set; }
public Guid LocalParentId { get; set; }
public FileSyncObject<T> LocalParent { get; set; }
public virtual ICollection<FileSyncObject<T>> LocalChildren { get; set; }
}
What's the best approach? Is this even possible?

EF Core name mapping

I've been trying to figure out how to do the following (although my research did not help): I have the these three classes:
public abstract class Classifier
{
public int ClassifierId { get; set; }
public string ClassifierName { get; set; }
public DateTime DateAdded { get; set; }
}
public class ManualClassifier : Classifier
{
public int ManualClassifierId { get; set; }
public string user_name { get; set; }
public string userName { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string email { get; set; }
public string password { get; set; }
}
public class ToolClassifier : Classifier
{
public int ToolId { get; set; }
public string ToolName { get; set; }
}
Both the ManualClassifier and ToolClassifer inherit from Classifier. I'm using EF Core to map this to a database but the question is the following: I've already searched a bit and I must make use of a descriminator which basically is an implicitly created column that will say the type of, in this case, classifier. So far so good. The issue arises when I have a property called ManualClassifierId as well as a ToolId. I want this two properties to map to the ClassifierId property. So in the table representing the entity Classifier, the ClassifierId property will either be the ManualClassifierId or the ToolId.
How can I achieve this mapping? Also, this solution would mean that both child classes would both have empty fileds in the tables (due to inheriting the three properties from the Classifier class). Is there a better solution? Perhaps just erase the Id's from both child classes a let them inherit the parent one?
Thank you in advance!
To use the same column name in both classes, you can add a Column attribute to both properties. Then they will both use that column name in the database. See ColumnAttribute(String).
Use it like this:
public class ManualClassifier : Classifier
{
[Column(Name="ClassifierId")]
public int ManualClassifierId { get; set; }
...........
}
Do the same with ToolId.

Passing values from concretized instance of an base class to another base class instance

I am currently working on making viewmodels capable of parsing data extracted from database to the UI and vice versa, and to do so I do a lot of manual mapping between my two viewmodels.
Currently I try to pass some values that determines an attribute, but since each attributetype requires specifying a lot specific parameter, and 90% of the variables will be redundant in all cases since attributes only have one type..
Thus i have create a placeholder base class, which just contains an Id, that each atttribute have,
and each of the specific attribute type parameter will then use this placeholder as base class..
example:
public class BooleanViewSpecification : AttributeTypeSpecification
{
public string TrueOptionText { get; set; }
public string FalseOptionText { get; set; }
}
public class DateTimeTypeViewSpecification : AttributeTypeSpecification
{
public DateTime EarliestDataTime { get; set; }
public DateTime LatestDataTime { get; set; }
}
and my Attribute class is just an
public class AttributeView
{
public DataType Type { get; set; }
public AttributeTypeSpecification AttributeTypeViewSpecification { get; set; }
}
And the same Goes for my DB view model
public class BooleanSpecification : AttributeTypeSpecification
{
public string TrueOptionText { get; set; }
public string FalseOptionText { get; set; }
}
public class DateTimeTypeSpecification : AttributeTypeSpecification
{
public DateTime EarliestDataTime { get; set; }
public DateTime LatestDataTime { get; set; }
}
and my Attribute class is just an
public class Attribute
{
public DataType Type { get; set; }
public AttributeTypeSpecification AttributeTypeSpecification { get; set; }
}
Problem is then mapping from one class to another class
public static IEnumerable<AttributeView> MapToViewModel(this IEnumerable<Attribute> attributes)
{
return attributes.Select(z => new AttributeView()
{
Type = z.Type,
AttributeTypeViewSpecification = z.AttributeTypeSpecification
});
}
Which does not seem to work?
I use entity framework and migrate using Code-First what I receive is the Id of the location, and not the actual values?
I cant seem to understand why I cant be given the values - if it during the mapping does have the value?
So why cant they be mapped over?
I retrieve the value
Context.Include(Attribute).ThenInclude(AttributeTypeSpecification)
The only thing I receive is the actual Id rather than the specified entries?

Multiple metadata classes for entity

Suppose I have 2 entities one is UserMaster another is ProjectMaster, now there are few common properties in both the entities and I need to apply same data annotations and attributes on both set of common properties which I do using metadata classes, now my question is there any way that I have a common metadata class for all this common properties across entities,so that I don't need apply data annotation and attributes for common properties again and again and if yes then how to achieve this.
You can create a base metadata class and the other metadata class derive from the base and add some other properties.
Example:
In the below example I supposed you can't derive Class2 from Class1 so I have inheritance only between metadata classes, otherwise you can simply have inheritance between your model classes.
[MetadataType(typeof(Class1Metadata))]
public class Class1
{
public string Id { get; set; }
public string Name { get; set; }
}
[MetadataType(typeof(Class2Metadata))]
public class Class2
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class Class1Metadata
{
[Display(Name="Id1")]
public string Id { get; set; }
[Display(Name = "Name1")]
public string Name { get; set; }
}
public class Class2Metadata:Class1Metadata
{
[Display(Name = "Description2")]
public string Description { get; set; }
}

How to add userId to every query without adding it to all domain classes?

I'm working with sqlite and the c# sqlite-net library.
Some of my entities:
public class Product
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[MaxLength(50)]
public string Name { get; set; }
[MaxLength(50)]
public string Brand { get; set; }
[MaxLength(50)]
public string Type { get; set; }
[MaxLength(50)]
}
public class ProductImage
{
public int Id { get; set; }
public string Name { get; set; }
public string Container { get; set; }
public int ProductId { get; set; }
}
Now all entities belong to a user and the user should always work only with his own entities. So when I insert an entity in the database I want to store the userId with it.
However... I don't want to add the userId to all my domain classes.
I'm storing my entities like this right now:
await _databaseManager.GetDatabaseInstance().InsertAsync(entity);
and selecting like this:
var products = await _databaseManager.GetDatabaseInstance().Table().ToListAsync();
So is there a way with sqlite-net to add the userId to the database without adding it to all domain classes?
Doesn't it support inheritance, as in looking at the type's hierarchy? I would suggest it would, or at least should. So, if it does you could use an abstract base class or an interface. Something like this:
public abstract class StandardEntity {
public int UserId { get; set; } // decorate with attributes as necessary
}
And inherit:
public class Product : StandardEntity {
}

Categories

Resources