Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 21 days ago.
Improve this question
I've come across an expression in some code and I can't find any place that explains it.
The code is:
public void Create(T entity) => RepositoryContext.Set<T>.Add(entity);
where T is a class type
RepositoryContext is a DbContext environment if that matters. It's the ".Set" I don't get.
Thanks in advance
Assuming this is a method call Set<T>() actually - as per docs Set method creates a DbSet for provided entity type:
Creates a DbSet<TEntity> that can be used to query and save instances of TEntity.
So it can be used to generically work entities of context. So for example:
public class MyAppContext : DbContext
{
public DbSet<MyEntity> MyEntities {get;set;}
}
MyAppContext ctx = ...
ctx.Set<MyEntity> is analogous to ctx.MyEntities.
Note that for example documentation mentions usage of this method as one of the strategies when working with nullable reference types so there is no warnings:
public class MyAppContext : DbContext
{
public DbSet<MyEntity> MyEntities => Set<MyEntity>();
}
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I have different Models in my Asp.Net Core application. Is it recommended to do SQL queries inside IActionResult Action methods? or is a better to have a seperate class do all work in terms of fetching data for each of the models and return the result to IActionResult method where we can display Return View()?
I have a static Utility class. If I were to have a class for each model to fetch data I would need access to IHttpContextAccessor but i cant assign that in a static class. What type of class would be the way to go?
You can go for the repository pattern (https://martinfowler.com/eaaCatalog/repository.html), adding a generic repository (one single class) or a concrete repository (one per model).
Another solution would be using partial classes, to add data access logic in your model classes.
In example:
public partial class MyModel
{
public string Name { get; set; }
}
public partial class MyModel
{
//add data access here
}
Also to identify them, you could add an extension to model class files, like ".cs.da" (then you would have "MyModel.cs" and "MyModel.cs.da" files) and nest them using File nesting extension: https://marketplace.visualstudio.com/items?itemName=MadsKristensen.FileNesting
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
In a database first application, I have a lot of tables under several schemas in the database, Lookup-tables and/or tables for a specific departments (or group of people). I am using C# under .NET Core 3; trying to group the tables in similar schemas in the application code but I can't find any suitable solution.
For example: In my database, there are 80 tables in lookup schema. In the c# code I want to be able to access these in a similar way, e.g. dbcontext.lookup.Tablexxx... or for marketing summary, dbcontext.marketing.summaryxxx etc. Is this possible? Any suggestion would be much appreciated.
You could intrododuce a nested class in your DbContext to hold the DbSet. Like this:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Tablexxx>();
base.OnModelCreating(modelBuilder);
}
public class Lookup
{
Db db;
public Lookup(Db db)
{
this.db = db;
}
public DbSet<Tablexxx> CreditAccounts => db.Set<Tablexxx>();
}
public Lookup Lookups => new Lookup(this);
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I'm using AutoMapper with Entity Framework. I have a hierarchy of entities:
Person
Student
Worker
Each business object has a map to an entity in the database. To convert the business objects into entities I'm using AutoMapper v6.2.2
I wonder if there is a better way to find the "best" mapping or do I really need to have something like this in code:
public PersonEntity MapPerson(Person person)
{
switch (person.Type)
{
case PersonType.Unknown:
return Mapper.Map<PersonEntity>(person);
case PersonType.Student:
return Mapper.Map<StudentEntity>(person);
case PersonType.Worker:
return Mapper.Map<WorkerEntity>(person);
default:
throw new ArgumentOutOfRangeException();
}
}
The good thing is, I already have a "Type" enum for discriminator and things like that but it still feels wrong. Maybe you can help.
You can do this...
var mapped = Mapper.Map(person, person.GetType(), typeof(PersonEntity));
Please refer to the AutoMapper docs for more details.
http://docs.automapper.org/en/stable/Mapping-inheritance.html
AutoMapper is capable of mapping inheritance. The mapping should look like this:
class PersonMapperProfile : AutoMapper.Profile
{
public PersonMapperProfile()
{
this.CreateMap<Student, StudentEntity>();
this.CreateMap<Worker, WorkerEntity>();
this.CreateMap<Person, PersonEntity>()
.Include<Student, StudentEntity>()
.Include<Worker, WorkerEntity>();
}
}
Now when you map a Person to PersonEntity, AutoMapper will create the correct base type or sub types.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to mock a method in the class
public class Test<T> where T : class
{
public string GetData()
return "Test"
}
}
How to mock the GetData class by using FakeItEasy? When I try to return a value using FakeITEasy it given me an message saying parameterless constructor
An example
var FakeTest = A.Fake<Test<DocumentTest>>();
A.CallTo(() => FakeTest.GetByIdAsync("Site"(A<string>.Ignored))).MustHaveHappened();
To fix a missing parameterless constructor just add
public Test() {}
to your class shown. That allows a mocking framework to new up an instance of the class and then set properties as needed.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
If a partial class has the same method name but different types I will get an error right?
public partial class Employee
{
public int sum()
{
}
}
public partial class Employee
{
public string sum()
{
}
}
Yes, it does:
'Employee' already defines a member called 'sum' with the same parameter types
Partial classes are just the same as regular class, except their definition is split in two sources. They have to comply to every rule regular classes have. That includes rules concerning the uniqueness of method signatures.