I've been working on a MVC5 website to manage insurance policies, and am stuck on what I think is a design problem. I have a code first, TPH entity situation with the following abstract class called policy:
public abstract class Policy
{
public int PolicyID { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime Inception { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime Expiration { get; set; }
}
And two concrete classes as follows:
public class UmbrellaPolicy : Policy
{
[Display(Name = "Umbrella:")]
public bool hasUmb { get; set; }
public virtual List<Umb> UmbCoverages { get; set; }
}
public class PackagePolicy : Policy
{
[Display(Name = "General Liability:")]
public bool hasGen { get; set; }
public virtual List<Gen> GenCoverages { get; set; }
[Display(Name = "Umbrella:")]
public bool hasUmb { get; set; }
public virtual List<Umb> UmbCoverages { get; set; }
}
And, finally a class which has a one to many relationship with both of the concrete classes above:
public class Umb
{
public int UmbID { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "Coverage Effective")]
public DateTime DateAdded { get; set; }
[Display(Name = "Each Occurrence Limit")]
public decimal OccurrenceLimit { get; set; }
[Display(Name = "Each Occurrence Retention")]
public decimal OccurrenceRetention { get; set; }
[Display(Name = "Aggregate Limit")]
public decimal AggregateLimit { get; set; }
public virtual Policy Policy { get; set; }
}
The business requirement is if a customer wants umbrella coverage, they purchase either an umbrella policy if they want only umbrella coverage or a package policy if they want umbrella coverage and liability coverage (like those commercials that want you to bundle homeowners and auto policies). The class that describes how much insurance a customer has (the class Umb) is the same regardless of whether the customer purchased umbrella coverage in a stand-alone policy or a package.
When I try to create a new Umb and add the policy (whether umbrella or package) to the virtual Policy property, and then try to view a list of Umb objects from the list on the concrete policy class, none show up.
After looking in the database, it appears that when I did my db migration there are three key columns: UmbrellaPolicy_PolicyID, PackagePolicy_PolicyID, and Policy_PolicyID. It successfully adds the correct policy id to the Policy_PolicyID column, but I guess entity framework looks at the Umbrella or Package column depending on which concrete class I am working with (i.e. in a strongly typed view). I feel like I'm missing something obvious or I went down a shaky implementation path. Any suggestions or readings someone could point me to would be awesome. Thanks!
Related
Hi I'm currently setting up my scoringProfile. I've marked by fields as "IsSearchable" as show below (Notice one field is a complex array).
public class MyIndexSchema
{
[IsSearchable] public int id { get; set; }
[IsSearchable] public string name { get; set; }
[IsSearchable] public string keywords { get; set; }
[IsSearchable] public Category[] category { get; set; }
}
public class Category
{
public int id { get; set; }
[IsSearchable] public string type { get; set; }
}
The weight I've defined is as below
var fieldWeights = new Dictionary<string, double>
{
{"name", 2},
{"category/type", 1}
};
But when i deploy my solution I'm seeing the following weights in the "Scoring profiles" tab on the selected index, but NOT the Category type field "category/type". What am I doing wrong?
To make the answer easier to be seen by others, I'm posting the answer revealed in comment:
This issue is a bug in the portal.The weights were properly applied even though its not showing up in the Portal.
I am building ParkingReservation in DDD, In short means that people can invite place and when the car get in the camera identify the model and update the status of the place.
I divided the model to three Bounded contexts:
The first is Reservation Context that include the following objects:
`public class Lot
{
public int ID { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
public List<Place> Places { get; set; }
}
public class Place
{
public int ID { get; set; }
public int FloorNumber { get; set; }
public int RowNumber { get; set; }
public int ParkingNumber { get; set; }
}
public class Car
{
public int ID { get; set; }
public string Model { get; set; }
}
public class Driver
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public int Age { get; set; }
public Gender Gender { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public bool AcceptAdsToMail { get; set; }
public byte[] PictureData { get; set; }
public DateTime RegistrationTime { get; set; }
public DriverStatuses DriverStatuses { get; set; }
}
public class Reservation
{
public int ID { get; set; }
public Driver Driver { get; set; }
public Car Car { get; set; }
public Place Place { get; set; }
public DateTime OrderTime { get; set; }
public DateTime ParkingStartTime { get; set; }
public DateTime ParkingEndTime { get; set; }
public ParkingStatuses ParkingStatus { get; set; }
}
public class ParkingHistory
{
public int ID { get; set; }
public Place Place { get; set; }
public Driver Driver { get; set; }
public Car Car { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
}`
The Parking Lot has list of places
the Driver reserve place through the application
the saved place saved in the Reservation object and when the parking time
elapsed, new parkinghistory added to parkinghistories list that belong to driver and car so you can watch history per car or driver.
for this context of Reservation:
(1) Is it correct to put Driver and Reservation for the Aggregate Roots? or maybe Lot too?
(2) Place is entity or value object?
Thank you
The main goal of your use case is scheduling. You need to think about a consistency boundary around that idea. To avoid time slots overlapping for a place in a lot you will need to create a new abstraction for the purpose.
"PlaceInLotReservations" sounds as a good option as an value object to serve as a factory for a Reservation aggregate. In order to represent reality of how scheduling works you should feed that aggregate in the context of a day, so "PlaceInLotReservationsRepository" should has a "findByDate" method that collects all Reservations for a place in a given datetime.
So the semantics would be something like:
val placeInLotReservations = PlaceInLotReservationsRepository.findBy(datetime)
val reservation = placeInLotReservations.reserveFor(car, driver, startingTime, endingTime)
ReservationsRepository.save(reservation)
If there are lot of reservations in a place and so race conditions you can even make the VO smaller by passing in day quarters instead of a day for the initial look up.
BTW, can and driver are VOs in the context of the Reservation aggregate (they are not aggregates).
You can also have the history by querying the Reservation repository, you donĀ“t need ParkingHistory.
Hope it helps.
I am trying to implement DbContext for couple of tables called 'Employee' and 'Department'
Relationship between Employee and Department is many to one. i.e. department can have many employees.
Below are the EntityFramework classes I designed ( CodeFirst approach )
[Table("Employee")]
public class Employee
{
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Column("Name")]
public string Name { get; set; }
[Column("Department_ID")]
public int Department_ID { get; set; }
public virtual Department Department { get; set; }
}
[Table("Department")]
public class Department
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
[Column("Name")]
public string Name { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
While adding Employee record I am getting below exception
"Invalid column name 'Department_ID1'."
I am not sure why EF is referring to Department_ID1. Do I need to add configuration in OnModelCreating method of DbContext?
I am using EF version 6.1.1
I've also gotten this problem in my EF one-many deals where the one has a List of the many property and my mapping didn't specify that property. For example take:
public class Notification
{
public long ID { get; set; }
public IList<NotificationRecipient> Recipients { get; set; }
}
then
public class NotificationRecipient
{
public long ID { get; set; }
public long NotificationID { get; set; }
public Notification Notification { get; set; }
}
Then in my mapping, the way that caused the Exception (the incorrect way):
builder.HasOne(x => x.Notification).WithMany()
.HasForeignKey(x => x.NotificationID);
What fixed it (the correct way) was specifying the WithMany property:
builder.HasOne(x => x.Notification).WithMany(x => x.Recipients)
.HasForeignKey(x => x.NotificationID);
Hi After spending some time I could fix this problem by using ForeignKey attribute on public virtual Department Department { get; set; } property of Employee class.
Please see below code.
[Table("Employee")]
public class Employee
{
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Column("Name")]
public string Name { get; set; }
[Column("Department_ID")]
public int Department_ID { get; set; }
[ForeignKey("Department_ID")]
public virtual Department Department { get; set; }
}
This fixed my problem. Are there any other solution to fix this? Using fluent API?
For me, the issue was resolved by removing a (duplicate?) virtual property.
Using the OP's example:
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public int Department_ID { get; set; }
public virtual Department Department { get; set; }
}
public class Department
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
Turns into:
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public int Department_ID { get; set; }
}
public class Department
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
In my case I added a virtual property on top of the auto generated property
I fixed it by adding the NotMapped attribute to my property, or you could configure with fluent api
public partial class Control
{
[NotMapped]
public virtual ICollection<Control> Children { get => this.InverseParent; set => this.InverseParent = value; }
}
I had the same error, my issue was the FK was a long but I had it as an int in the model. EF generated a new column because it didn't match types on the FK so it assumed they weren't the same and went ahead with making another one but putting 1 at the end because there was already one with the proper name. Making sure the types matched resolved the issue for me.
This can be fixed simply by putting [NotMapped] annotation on your virtual properties.
public class Employee
{
[ForeignKey("Department")]
public int Department_ID
[NotMapped]
public virtual Department Department { get; set; }
}
And in you modelBuilder:
modelBuilder.Entity<Employee>(entity =>
{
entity.HasOne(e => e.Department);
});
Just flip this around if you want to call by Department.
We use the [NotMapped] annotation so that EF Core will disregard it when looking at your database.
I'm having an issue with the ServiceStack implementation of Swagger with regards to the documentation of generic-typed response objects. Strongly-typed response objects are correctly documented and displayed, however once a generic-typed object is used as a response, the documentation is inaccurate and misleading.
Request DTO
[Route("/users/{UserId}", "GET", Summary = "Get a specific User Profile")]
public class GetUser : IReturn<ServiceResponse<UserProfile>>
{
[ApiMember(Description = "User Id", ParameterType = "path", IsRequired = true)]
public int UserId { get; set; }
}
Response DTO
public class ServiceResponse<T> : IServiceResponse<T>
{
public IList<string> Errors { get; set; }
public bool Successful { get; set; }
public string Message { get; set; }
public string StackTrace { get; set; }
public T Data { get; set; }
public ServiceResponse()
{
Errors = new List<string>();
}
}
Response DTO Type
public class UserProfile : RavenDocument
{
public UserProfile()
{
Races = new List<UserRace>();
Workouts = new List<Workout>();
}
public string FirstName { get; set; }
public string LastName { get; set; }
public string DisplayName { get; set; }
public DateTime? BirthDate { get; set; }
public Gender? Gender { get; set; }
public string UltracartPassword { get; set; }
public string UltracartCartId { get; set; }
[UniqueConstraint]
public string Email { get; set; }
public string ImageUrl { get; set; }
public FacebookUserInfo FacebookData { get; set; }
public GoogleUserInfo GoogleData { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime? LastUpdated { get; set; }
public UserAddress ShippingAddress { get; set; }
public UserAddress BillingAddress { get; set; }
public IList<UserRace> Races { get; set; }
public IList<Workout> Workouts { get; set; }
}
The examples are pretty straight forward. Nothing really hacky or clever going on, however this is the sample documentation I get from Swagger out of the box:
As you can see, the generic type isn't documented correctly and some other type is used instead. As I am using this same ServiceResponse wrapper for all my responses, this is happening across the board.
As you have found, the ServiceStack swagger plugin does not currently attempt to handle generic types cleanly. A simple alternative that should work better is to make concrete subclasses of the generic types. e.g.:
public class UserProfileResponse : ServiceResponse<UserProfile> { ... }
public class GetUser : IReturn<UserProfileResponse> ...
This should be handled properly by Swagger.
I've found generic types aren't always a great fit for ServiceStack DTOs. You'll find many discussions (for example here, here and here) on StackOverflow that discuss this, and the reasons why concrete types and generally avoiding inheritance is a good idea for ServiceStack DTOs.
It takes effort to overcome the temptation to apply the DRY principle to request/respone DTOs. The way I think about it is that generics and inheritance are language features that facilitate implementation of algorithms in generic, reusable ways, where the generic method or base class doesn't need to know about the details of the concrete type. While DTOs may superficially have common structures that look like opportunities for inheritance or generics, in this case the implementation and semantics of each DTO is different for each concrete usage, so the details of each request/response message deserve to be defined explicitly.
I'm having an issue with my ServiceStack w/ Swagger implementation regarding documenting required/optional properties. Developers implementing clients that consume my services love the Swagger documentation, however they don't know which properties are required vs. optional--aside from getting a 400 response on each attempt to get a valid request through.
Take the following example:
public class UserProfile
{
public string FirstName { get; set; }
public string LastName { get; set; }
public UserAddress Address { get; set; }
}
public class UserAddress
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Country { get; set; }
public string PhoneNumber { get; set; }
}
Swagger will cleanly show both of these types if they are part of my DTO, however I can't convey that FirstName, LastName, or any of the Address properties are required or not. Is there a way to accomplish this without having to roll a separate spec document?
You can use an [ApiMember(IsRequired = false)] attribute on the properties in the DTO to add extra information for swagger ui.
There is list of the attributes that swagger ui will recognise on the servicestack wiki