I have an Entity Framework Code First model with a column that is not mapped which I still want to persist between the server and the client. The model looks similar to this with many more properties:
public class OwnerInformation
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[MaxLength(16)]
public byte[] SSNEncrypted { get; set; }
[NotMapped]
[MaxLength(9)]
[MinLength(9)]
public string SSN { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
When the metadata is retrieved by Breeze SSN is not part of it, but when the data is sent over the wire the SSN is there. I would like to let breeze deal with the mapping through the metadata, but I would like to be able to still pass SSN between the client and the server and track it's state as I need to encrypt it before it is saved to the DB.
I tried adding it after the metadata is fetched like this:
var ownerType = manager.metadataStore.getEntityType('OwnerInformation');
var sSN = new breeze.DataProperty({
name: 'sSN',
dataType: breeze.DataType.String,
isNullable: false,
maxLength: 9
});
ownerType.addProperty(sSN);
Run Code Online (Sandbox Code Playgroud)
but I get the error: The 'OwnerInformation:#Models' EntityType has already been added to a MetadataStore and therefore no additional properties may be added to it.
Maybe I'm overthinking this and there is an easier way. I'm opened to any suggestions.
我采用了另一种方法,并决定在服务器上的运行时更改元数据。这是我的方法。
public class MyContextProvider : EFContextProvider<MyContext>
{
protected override string BuildJsonMetadata()
{
string metadata = base.BuildJsonMetadata();
JObject json = JObject.Parse(metadata);
var entityOwnerInfo = json["schema"]["entityType"].Children().Where(j => (string)j["name"] == "OwnerInformation").SingleOrDefault();
var propertyArray = entityOwnerInfo["property"] as Newtonsoft.Json.Linq.JArray;
JToken ssnPropertyType = JToken.Parse(@"{
""name"": ""SSN"",
""type"": ""Edm.String"",
""fixedLength"": ""true"",
""maxLength"": ""9"",
""minLength"": ""9"",
""nullable"": ""false""}");
propertyArray.Add(ssnPropertyType);
return json.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
683 次 |
| 最近记录: |