EF Core annotation for client side generated values - ef-code-first

I'd like to set my code-first class (EF Core 3.1) to have generated values with value generators (Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator). It's important to me that the values are generated on the client side and not on the database side. Also, I would like to use an annotation instead of the fluent API because I need to use derived types; something like this:
public class typeX
{
[Key]
[some-annotation?] // This is where I want to add my annotation
public string MyKey { get; set; }
[some-other-annotation?] // This is where I want to add my annotation
public string OtherData { get; set; }
}
public class derivedA : typeX
{
}
public class derivedB : typeX
{
}
I'd like to annotate MyKey and OtherData so they will use generators (e.g. GuidValueGenerator, MyCustomValueGenerator). I think I know how to use the fluent API for that (although most examples are for database side generation), but I can't find a way to use annotations. Is this possible? I'd like to generate values on creation in some cases and on creation+update in others.
Thanks

Related

Ardalis CleanArchitecture - BaseEntity & Composite Keys for AggregateRoots

What is the recommendation for extending BaseEntity as an AggregateRoot is I need to have a composite Key?
Essentially I need to be able to manage an Entity that would normally be configured in EFCore like:
builder.HasKey(z => new { z.PartA, z.PartB });
Currently the template gives us the following definition of BaseEntity, but it's unclear to me how I might need to setup the EntityTypeCuilder config, and how the Aggregates might handle this scenario under the hood.
// This can be modified to BaseEntity<TId> to support multiple key types (e.g. Guid)
public abstract class BaseEntity
{
public int Id { get; set; }
public List<BaseDomainEvent> Events = new List<BaseDomainEvent>();
}
Paul

Ignore column but populate property through stored procedure EF code first

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.

EF Code First: One-to-One One-way Relationship

I regularly have the following structure:
MyClass
public virtual ICollection<Version> Versions { get; set; }
public virtual Version CurrentVersion { get; set; }
That is, there is a list of stuff, and some class both points to that list, and one specific item in that list - either the current version of many versions, the next upcoming event in a list of events, etc.
In my schema what I'd like to end up with is a Foreign Key pointing from Version to MyClass - that much works out properly. But then I'd like a Foreign Key pointing from MyClass to Version representing the CurrentVersion property, with no Foreign Key pointing back - I don't want the extra storage or bother of telling a Version what MyClass it's the CurrentVersion for, if any. Put another way, I'd like this second relationship to be one-way from MyClass to Version, even though it's one-to-one.
What EF Code First gives me instead is the normal one-to-many on the first property, with the FK from Version to MyClass, but then a full one-to-one relationship on the second property with an FK pointing in both directions - so the underlying schema for Version ends up with MyClass_Id and MyClass_Id1.
So, is there a way to get a one-way relationship in EF Code First without resorting to the Fluent API? It looked like maybe System.ComponentModel.DataAnnotations.Schema.InverseProperty had a shot at it, but it didn't seem to offer a way to say "Don't generate one."
The key is to specify the InverseProperty on the property that points back, so that EF realizes it's to the Many-to-Many, not to the One-to-One.
public class MyClass
{
public int Id { get; set; }
public Version CurrentVersion { get; set; }
public ICollection<Version> Versions { get; set; }
}
public class Version
{
public int Id { get; set; }
[InverseProperty("Versions")]
public Versioned Versioned { get; set; }
}

Auto Generating partial classes

This is my first time using EF in VS2012 as I have been using 2010 for up until now. I have added the entity framework model and it adds 2 files with the extension .tt which I am sure was not present in VS2010. Under one of these it generates partial classes to match the entities. However I already have these partial classes in another manually created folder called Entites under the root of my app. This causes an issue on build as they conflict...
How do I either either stop them autogenerating or how do I make them play nice with my manually created partial classes? It is incredibly annoying that VS2012 does this without asking as it breaks my code!
Example of Auto Generated class
namespace StatisticsServer
{
using System;
using System.Collections.Generic;
public partial class Statistic
{
public int StatID { get; set; }
public int CategoryID { get; set; }
public int FranchiseID { get; set; }
public double StatValue { get; set; }
}
}
Example of Manually created class
namespace StatisticsServer.Entities
{
public partial class Statistic
{
public static List<Statistic> GetStatisticsSet(int categoryID)
{
List<Statistic> statSet = new List<Statistic>();
using (var context = new StatisticsTestEntities())
{
statSet = (from s in context.Statistics where s.CategoryID == categoryID select s).ToList();
}
return statSet;
}
}
}
Make sure that your manually created classes are in the same namespace as the auto-generated ones.
Otherwise the two classes will be seen as separate partial classes, and if you use both namespaces in the same calling class it cannot determine which class you mean.
So for example in your case you might have:
using StatisticsServer;
using StatisticsServer.Entities;
When you then declare an object of the type Statistic in that class the build will fail because the Statistic class exists in both namespaces.

During JSON Serialization in .NET, is there a way to specify how "deep" the object graph you wish to go?

I have some complex object graphs, when I want to send them down to the client, I'm creating a separate DTO and serializing the objects into that. This is a pain in the ass. Is there anyway to serialize objects and only say, "Go one references deep" so if I have an object:
public class Test {
public Project { get; set; }
}
public class Project {
public int Id { get; set; }
public Vendor Vendor { get; set; }
}
If I go to serialize Test it won't go to the Vendor, but it'll correctly serialize the Project. I realize I can add an annotation for JsonIgnore, but if I were serializing all Projects, I might want a Vendor.
I think you're going to have to do some custom extension work: I found a conversation and some samples at http://json.codeplex.com/Thread/View.aspx?ThreadId=24459

Resources