I'm using dotnet ef scaffold to generate model classes from an existing database "Microsoft.EntityFrameworkCore.Tools"
Everything works and models get generated with data annotations as expected and asp.net controller returns Json no issues.
using the [JsonIgnore]
decorator helps to remove the fields that I do NOT want the api to return. (like Id's etc)
I would like to use a partial class that is not gonna be overwritten when you scaffold a new DbContext. (bonus if it could be in another assembly) Newtonsoft lets me handle nulls,dates and reference loops - I would like all that goodness to be kept separate from all EF's annotations.
I know there are other techniques, but this seems like a good use case for Partial classes ... like with the jQuery unobtrusive validation that comes with ASP.Net MVC out the box ... but I've hit a roadblock on this one ... so is this even possible or am I missing something obvious :|
Model generated by Entity Framework
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Newtonsoft.Json;
namespace SomeNamespace
{
public partial class Media
{
public int Id { get; set; }
public int SiteId { get; set; }
public Guid Ref { get; set; }
[Required]
[StringLength(2000)]
public string Url { get; set; }
public string Caption { get; set; }
[ForeignKey("SiteId")]
[InverseProperty("Media")]
public Site Site { get; set; }
}
}
Partial Class
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Mvc; // "ModelMetadataType"
using Newtonsoft.Json;
namespace SomeNamespace
{
[ModelMetadataType(typeof(MediaMetaData))]
public partial class Media
{
}
public partial class MediaMetaData
{
[JsonIgnore]
public int Id { get; set; }
[JsonIgnore]
public int SiteId { get; set; }
[JsonIgnore]
public Site Site { get; set; }
}
}
:( => UPDATE : 23 Jan 2018
I get the comment from this answer How to use ASP.Net core ModelMetadata attribute
"because you are trying to use the metadata type of a derived class to rename the properties in a base type"
I can appreciate Liskov, but then is a Partial Class is actually a Derived Class ??
What I don't get is that the [JsonIgnore]
works when applied in the EF model like so ...
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Mvc; // "ModelMetadataType"
using Newtonsoft.Json;
namespace SomeNamespace
{
public partial class Media
{
[JsonIgnore]
public int Id { get; set; }
[JsonIgnore]
public int SiteId { get; set; }
public Guid Ref { get; set; }
[Required]
[StringLength(2000)]
public string Url { get; set; }
public string Caption { get; set; }
[JsonIgnore]
[ForeignKey("SiteId")]
[InverseProperty("Media")]
public Site Site { get; set; }
}
}
and produces the following ...
{
"ref": "0c0e1475-63a1-41ce-9c6f-b4be6afd516a",
"url": "https://drive.com/images/fb65a200",
"caption": "bla bla"
}
it's what I want... but that gets overwritten when using code generation (the problem that partial classes were supposed to solve)
but by using partial classes as in my original question above I get this...
{
"id": 7,
"siteId": 2000,
"ref": "0c0e1475-63a1-41ce-9c6f-b4be6afd516a",
"url": "https://drive.com/images/fb65a200",
"caption": "bla bla",
"site": {...}
}