@enchev, yes. im working on changing to RBS.
The DBContext must be overwritten. All clear.
But Scaffolding changes the Names of Navigation Properties. And this is horrible.
Let me explain...
You got a Table (Cars) with 2 FK's to another Table (Drivers)
because one Car can have 2 Drivers (Main Driver and Passenger)
Tables:
Car
CarId INT IDENTITY(1, 1) NOT NULL
DriverMainId INT NULL
PassengerId INT NULL
... and whatelse
Driver
DriverId INT IDENTITY(1, 1) NOT NULL
Name NVARCHAR(50)
... and whatelse
Scaffolding generates Model Class
Car.cs
[Table("Car", Schema = "dbo")]
public partial class Car
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CarId
{
get;
set;
}
public int DriverMainId
{
get;
set;
}
public Driver Driver { get; set; }
public int PassengerId
{
get;
set;
}
public Driver Driver1 { get; set; } // Scaffolding only sets numbers behind the Tablename
}
Now you are programming code like
string GetMainDriverNameFromCar(int carId)
{
return (from qry in dbcontext
.Cars
.Include(x=>x.Driver) // The MainDriver Navigation Property
where qry.CarId == carId
Selectqry.Driver.Name).SingleOrDefault();
}
string GetPassengerNameFromCar(int carId)
{
return (from qry in dbcontext
.Cars
.Include(x=>x.Driver1) // The Passenger Navigation Property
where qry.CarId == carId
Selectqry.Driver1.Name).SingleOrDefault();
}
All fine. You wrote large Amount of Code that will use the Driver and Driver1 NavProperty cause you know... Driver = MainDriver Driver1 = Passenger.
But now... You modify the Car Table. Insert another FK
Car
CarId INT IDENTITY(1, 1) NOT NULL
DriverMainId INT NULL
PassengerId INT NULL
... and whatelse
Passenger2Id INT NULL
And scaffolding may change the Navigation Property Name like this:
Car.cs
[Table("Car", Schema = "dbo")]
public partial class Car
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CarId
{
get;
set;
}
public int DriverMainId
{
get;
set;
}
public Driver Driver1 { get; set; }
public int PassengerId
{
get;
set;
}
public Driver Driver2 { get; set; } // Scaffolding only sets numbers behind the Tablename
public int Passenger2Id
{
get;
set;
}
public Driver Driver { get; set; } // Scaffolding only sets numbers behind the Tablename
}
Now:
Driver is the New Second Passenger
Driver1 is the DriverMain
Driver2 is the Passenger
Youre existing code compiles fine. But the Data you get from the existing Methods is completly wrong!
- GetMainDriverNameFromCar(int carId) returns the Name of the new Second Passenger
- GetPassengerNameFromCar(int carId) returns the Name of the DriverMain
But you dont notice that at the first Look. Only Testing reveals that.
And then you cann change all existing Code that accesses Driver & Driver1 to
-Driver is now Driver1
-Driver1 is now Driver2
That costs hours and features must be tested again that runs perfect for ages.
That is what Ef Scaffolding do.
THIS HAS NOTHING TO DO WITH Radzen BS OR Radzen Studio!
But there is a way out of this.
From StackOverFlow:
Extend IDesignTimeServices and add a service for ICandidateNamingService. This allows you to control the naming of the Navigation Properties during scaffolding. In the CustomCandidateNamingService class, you can define your own logic for generating the navigation property names. For example, you could check if a navigation property name already exists and, if so, append a unique identifier instead of a number.
This is what i mean.
My question is:
Can you implement the possibility that i can write such a "custom" scaffolding and generate the Model Classes and Context manually
without losing the Methods in the Radzen generated DBService.c (Getxxx, Deletexxx, Updatexxx, ect.)
The Scaffolding (EF Generates Model Classes & DBContext Class) and the Radzen Generation of DBService.cs
runs now in 1 Step (Radzen: Infer and Generate).
What i wish is that these Processes can be done or not in 2 Steps.
I will do the scaffolding and then Radzen: Generate the DBService.cs without Infering (Scaffolding) the Database.
Perhaps from the DBContext Class that i generate.
Kind regards
Thomas