I have:
Entity Framework DB first
public partial class Departments
{
public string Name_Department { get; set; }
public int Department_ID { get; set; }
}
When i was refreshed model all modify in file was cleared
I need:
How can i add attribute property to Serialize XML like:
[Serializable]
[XmlType(nameof(Departments))]
public partial class Departments
{
[XmlAttribute("Name_Department")]
public string Name_Department { get; set; }
[XmlAttribute("Department_ID")]
public int Department_ID { get; set; }
}
You have to make the generated properties private in the properties window. Then you have to create new public properties that reference the private properties and add the attributes to them. In the properties window you can also rename your now private properties so that you can name your public properties like the original ones:
Generated code:
public partial class Departments
{
private string Name_DepartmentCore { get; set; }
private int Department_IDCore { get; set; }
}
Custom code (in C# 7.0):
[Serializable]
[XmlType(nameof(Departments))]
public partial class Departments
{
[XmlAttribute("Name_Department")]
public string Name_Department {
get => Name_DepartmentCore;
set => Name_DepartmentCore = value;
}
[XmlAttribute("Department_ID")]
public int Department_ID {
get => Department_IDCore;
set => Department_IDCore = value;
}
}
Your class model is generated when you are using DB first, that is by design.
Solution 1. Microsoft is dropping support for database first, and edmx is depricated. One solution would be to use CODE FIRST instead. This would allow you to decorate your POCO classes with attributes.
Solution 2. Create a parallel model just for XML serialization.
Related
I have 3 classes with the name of Employee, EmployeeTwo, and EmployeeThree, I wanted to map Employee to EmployeeTwo and Employee to EmployeeThree.
Following are the Classes.
I also have tried to define the AdaptMember attribute on the Employee Class property but it only defines one and I have to map it with two different.
Note: I cannot define any attribute to the EmployeeTwo and EmployeeThree classes because this comes from the API and it can be regenrate.
public class Employee
{
[AdaptMember(nameof(EmployeeTwo.EmployeeID))]
public int ID { get; set; }
[AdaptMember(nameof(EmployeeTwo.EmployeeName))]
public string Name { get; set; }
}
public class EmployeeTwo
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
}
public class EmployeeThree
{
public int EmployeeThreeID { get; set; }
public string EmployeeThreeName { get; set; }
}
Any help is really appriciated.
As I understand you, you want to make a custom object mapping using mapster.
If you want to make a custom mapping you need to create a class:
using Mapster;
namespace Application.Mapsters
{
public class Config : ICodeGenerationRegister
{
public void Register(CodeGenerationConfig config)
{
TypeAdapterConfig<Employee, EmployeeTwo>
.NewConfig()
.Map(dst => dst.EmployeeID, src => src.ID)
.Map(dst => dst.EmployeeName, src => src.Name);
}
}
}
For instance, follow this example to see how to make a custom mapping using mapster:
https://floatincode.net/2021/07/26/mapster-generate-dto-async-after-map-actions-dependency-injection/
I have a need for a project to allow the user to setup database info and table names in the config file. I want to use ADO.NET Entity Model to use the LINQ and just stay away from SQL the most I can to make it easier on myself. Is there a way to dynamically assign what table a Class needs to access for the modal?
For example:
This is what it looks like normally
[Table("database.table")]
public partial class table
{
[Key]
[Column(TypeName = "usmallint")]
public int ID { get; set; }
[Required]
[StringLength(128)]
public string Instance { get; set; }
[Required]
[StringLength(60)]
public string Name { get; set; }
}
I want to be able to dynamically set the TableAttribute so the model knows what table to access for the class.
[Table(Config.DBName + Config.tableName)]
public partial class table
{
[Key]
[Column(TypeName = "usmallint")]
public int ID { get; set; }
[Required]
[StringLength(128)]
public string Instance { get; set; }
[Required]
[StringLength(60)]
public string Name { get; set; }
}
Any help or letting me know that it is not possible would be appreciated.
I've not tested this, but I think you can do this via implementing custom conventions - if you're using EF6 at least.
First, you need to create a custom Convention:
public class CustomTableNameConvention : IStoreModelConvention<EntitySet>
{
private readonly string _tablePrefix;
public CustomTableNameConvention(string tablePrefix)
{
_tablePrefix = tablePrefix;
}
public void Apply(EntitySet item, DbModel model)
{
//change table name.
item.Table = $"{_tablePrefix}" + item.Table;
}
}
Next, you need to add this convention in the OnModelCreating method of your Context:
public class MyContext : DbContext
{
public MyContext(string connectionString) : base(connectionstring)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//get the dynamic table prefix...
var myAppPrefix = "user1";
modelBuilder.Conventions.Add(new CustomTableNameConvention(myAppPrefix));
base.OnModelCreating(modelBuilder);
}
public DbSet<SomeModel> { get; set; }
...
}
... Then, whenever the model in this application instance starts up, it should run through the above when deciding what the table name(s) should be.
Just replace the myAppPrefix = ... code with a call to an appropriate service to get the prefix for this instance.
the obvious caveat with this is that you cannot use a value for the prefix which is returned from the database (at least, not via this Context), as the Context isn't yet initialised.. so you'd have to either store it in settings or pass it in some other way.
Hope this helps.
I have two classes below, Form and FormFieldOption<T>. The latter is intended to represent a set of <option> inside of a <select> in a web form that the Form object represents.
public class Form
{
public int Id { get; set; }
public FormFieldOption<string> Status { get; set; }
public FormFieldOption<string> Category { get; set; }
public FormFieldOption<int> Severity { get; set; }
}
public class FormFieldOption<T>
{
public int Id { get; set; }
public string Description { get; set; }
public bool Active { get; set; }
public T Value { get; set; }
}
I'm working on configuring these entities for Code-First like below:
public class FormFieldOptionStringConfiguration : EntityTypeConfiguration<FormFieldOption<string>>
{
public FormConfiguration()
{
//configure here
}
}
Unfortunately at this point I've discovered that I would like there to be a difference between the configuration for the Status and Category properties. I've not been able to figure out a way top configure the properties that utilize the FormFieldOption class individually. How could I do this?
Solutions I've come up with:
Create a new class that inherets FormFieldOption<T> for each property and configure individually.
Create an interface IFormFieldOption<T> and implement classes individually for each use.
I don't particularly care for either of these options as it feels like a lot of duplicated code just to create a different configuration for each table.
It turns out that you can't do this. What I ended up doing was creating a generic interface that I implemented for each property.
I have a following model class:
public class Class1
{
public int Id { get; set; }
[StringLength(50)]
public string Name { get; set; }
}
public class MyContext : DbContext
{
public DbSet<Class1> Class1s{ get; set; }
}
Now I want to add one more property to my model that will be
public string NewProp { get; set; }
I know very well to achieve this by code , but is there any way to add this dynamically?
in simple I want to make a UI who ask me about property name as well as datatype when I submit values it automatically add the property to my existing model. I am using EntityFramework Code-First. However since I have already the DB, I can also use the DB-First approach if the solution is using DB-First.
I have the following classes in my Model:
public abstract class Entity : IEntity
{
[ScaffoldColumn(false)]
public int Id { get; set; }
[Required,StringLength(500)]
public string Name { get; set; }
}
and
public class Model : SortableEntity
{
[Required]
public ModelType Type { get; set; }
[ListRequired]
public List<Producer> Producers { get; set; }
public List<PrintArea> PrintAreas { get; set; }
public List<Color> Colors { get; set; }
}
To display the "Model" class in the view I simply call Html.EditorFor(model=>model), but the "Name" property of the base class is rendered last, which is not the desired behaviour.
Is it possible to influenece on the order of displayed fields somehow?
I've not been able to find an attribute for that, so your options are:
1) create one, and then revise the base Object.ascx template to account for it, or
2) create a custom editor template for your classes that explicitly put stuff in the order you want.