How to specify a Schema while mapping with DapperExtensions? - c#

I'm trying to get all records from SQL database using DapperExtensions.
But I have a Schema set to other than dbo for some tables. Hence, the table is not recognized from sql query.
For example, a table is in the form [Schema][TableName]. But when I start query, error is thrown like:
Invalid object name 'TableName'.
This is the Model class:
using System;
using Dapper.Contrib.Extensions;
using ImOnTech.Teftis.Core.Models;
using ImOnTech.Teftis.Core.Models.DT_Inspection;
namespace ImOnTech.Teftis.Core.Models.DT_Inspection
{
[Table("DT_Inspection.City")]
public class City
{
This is the function to GetAll records from database:
public async Task<IReadOnlyList<City>> GetAllAsync()
{
var CityList = await Context.Connection.GetListAsync<City>();
Context.Connection.Close();
return CityList.ToList();
}

While mapping your models, be bit more explicit. Mention the Schema explicitly.
Following is an example how to provide various mapping properties.
public class Customer
{
public int CustomerID { get; set; }
public string Name { get; set; }
}
public sealed class CustomerMapper : ClassMapper<Customer>
{
public CustomerMapper()
{
Schema("dbo");
Table("Customer");
Map(x => x.CustomerID).Key(KeyType.Identity);
AutoMap();
}
}
Please note that, if your column names and property name in model is same, you do not need to call Map for each property (the way I did above Map(x => x.CustomerID).Key(KeyType.Identity);). Instead, only call AutoMap(); and properties will be automatically mapped.
To make these mappings known to Dapper Extensions, call the following code only once at application startup:
DapperExtensions.DapperExtensions.SetMappingAssemblies(new[] { Assembly.GetExecutingAssembly() });

Related

C# Cassandra UDT Mappings Configuration Better Approach

Is there any better way to Write UDTMaps is the same way as Tables using Cassandra Database with C# Driver
Suppose there is a Table called Users(Id, name text, Address frozen )in Cassandra Database.
now to map this to C#
for Tables I can write as below
public class AppMappings : Mappings
{
public AppMappings()
{
For<User>()
.TableName("Users")
.Column(u => u.Id, cm => cm.WithName("userId"))
.Column(u => u.Name, cm => cm.WithName("username"))
.Column(u => u.AddressDetails , cm => cm.WithName("address"))
.PartitionKey(u=> u.Id);
}
}
public class User
{
public Guid Id {get;set;}
public string Name {get;set;}
public Address AddressDetails {get;set;}
}
public class Address {
public string State {get;set;}
public string City {get;set;}
}
Now I can Apply any Configurations with Single Class AppMappings , it can have many mappings with tables. With this one line I can initialize all mappings
MappingConfiguration.Global.Define<AppMappings>();
but inroder to apply UDT I need to add the below line manually.
session.UserDefinedTypes.Define(UdtMap.For<Address>("address_udt"));
Assume I have 10/20 UDTs then I need to add this line for every UDT. instead is there any way to add UDT Mappings in one place like Mappings?
or better approach to add UDT Mappings ?
Unfortunately the driver doesn't have an utility like that one for UDT mappings but you can create something similar like this:
public abstract class CustomUdtMappings
{
private readonly IDictionary<Type, UdtMap> _definitions = new Dictionary<Type, UdtMap>();
public UdtMap[] Definitions => _definitions.Values.ToArray();
public UdtMap<TPoco> For<TPoco>(string udtName = null, string keyspace = null) where TPoco : new()
{
if (_definitions.TryGetValue(typeof(TPoco), out var map) == false)
{
map = UdtMap.For<TPoco>(udtName, keyspace);
_definitions.Add(typeof(TPoco), map);
}
return (UdtMap<TPoco>) map;
}
}
Then create your mappings class that contains the udt mappings:
public class MyAppUdtMappings : CustomUdtMappings
{
public MyAppUdtMappings()
{
For<Udt1>("udt1")
.Map(udt => udt.Id, "iid")
.Map(udt => udt.Column1, "aasd");
For<Udt2>("udt2")
.Map(udt => udt.Id, "iiid")
.Map(udt => udt.Column1, "aaasd");
}
}
And you can use it this way:
await session.UserDefinedTypes.DefineAsync(new MyAppUdtMappings().Definitions).ConfigureAwait(false);
I think it makes a lot of sense to add something like this to the driver so I created https://datastax-oss.atlassian.net/browse/CSHARP-897 to track this.

.Net Core Entity Framework - Discrimator TPH

I'm currently trying to write to a table which inherits from an abstract base class. When I try to do this I get the following error (The ContactMethod property is the discriminator):
System.Data.SqlClient.SqlException: Invalid column name 'ContactMethod'.
EmailContactDetails.cs:
public class EmailContactDetail : ContactDetail
{
[ApiMember(Description = "The Contact Method")]
public override ContactMethod ContactMethod => ContactMethod.Email;
[ApiMember(Description = "Email Address")]
public string EmailAddress { get; set; }
}
EmailContactDetailConfiguration.cs:
public class EmailContactDetailsConfiguration : IEntityTypeConfiguration<EmailContactDetail>
{
public void Configure(EntityTypeBuilder<EmailContactDetail> builder) => Configure(builder, "dbo");
public void Configure(EntityTypeBuilder<EmailContactDetail> builder, string schema)
{
builder.Property(x => x.EmailAddress).HasColumnName("EmailAddress").HasColumnType("nvarchar(255)");
}
}
ContactDetail.cs:
public abstract class ContactDetail
{
[ApiMember(Description = "The Identifier")]
public Guid Id { get; set; }
[ApiMember(Description = "The Contact Method")]
public virtual ContactMethod ContactMethod { get; set; }
}
ContactDetailConfiguration.cs
public class ContactDetailsConfiguration : IEntityTypeConfiguration<ContactDetail>
{
public void Configure(EntityTypeBuilder<ContactDetail> builder) => Configure(builder, "dbo");
public void Configure(EntityTypeBuilder<ContactDetail> builder, string schema)
{
builder.ToTable("ContactDetails", schema);
// Table per hierarchy. all subclasses share the same db table for performance.
builder.HasDiscriminator(x => x.ContactMethod)
.HasValue<EmailContactDetail>(ContactMethod.Email);
builder.Property(x => x.Id).HasColumnName("Id").IsRequired().HasColumnType("uniqueidentifier").ValueGeneratedOnAdd();
}
}
I've tried hiding the discriminator "ContactMethod" by adding the following to the ContactDetailConfiguration.cs file:
builder.Ignore(x => x.ContactMethod);
Once I've done that I end up with the following error
The entity type 'EmailContactDetail' is part of a hierarchy, but does not have a discriminator property configured.
You shouldn't hide the property configured as TPH discriminator from EF because it is essential for EF Core implementation of the TPH strategy.
The initial error simply indicates that your model and database are out of sync. It's true that by convention EF Core uses string shadow property and column called Discriminator. But the whole purpose of HasDiscriminator fluent API is to allow changing the discriminator property/column type, as well as mapping it to an existing property of your entity model.
Which is the case here. You've told EF Core to use your existing property ContactMethod as discriminator, hence EF Core is looking for column named ContactMethod in the database table. So to resolve the issue, simply update your database from the model (using the usual procedure when model is changed - add new migration, update database etc).

Entity Framework Self Referencing Using Non-Primary Key Column

I have an employee table that self references to determine organization structure. I'm having some trouble trying to set this up using Code-First (POCO) fluently.
An employee record has both a "Position" field and a "ReportsTo" field and neither of the columns are the primary key (employee.id).
An employee with a "ReportsTo" value of "08294" , is an employee of a direct report of an employee with "Position" value of "08294".
Can anyone offer up some info on how to set this up using EF code first, fluently...is it possible?
I tried the code below and am getting error:
Employee_Employees_Source_Employee_Employees_Target: : The types of
all properties in the Dependent Role of a referential constraint must
be the same as the corresponding property types in the Principal Role.
The type of property 'ReportsTo' on entity 'Employee' does not match
the type of property 'Id' on entity 'Employee' in the referential
constraint 'Employee_Employees'.
Employee.cs
public class Employee
{
public int Id { get; set; } //pk
public string Position { get; set; } // i.e. 06895
public string ReportsTo{ get; set; } // i.e. 08294
public virtual Employee Supervisor { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
DbContext
modelBuilder.Entity<Employee>()
.HasMany(e => e.Employees)
.WithOptional(e => e.Supervisor)
.HasForeignKey(e => e.ReportsTo);
I think more than anything, I would like to keep the POCO free of EF "stuff" and be able to do something like:
employee.IsSupervisor(); // based on child employee count.
The issue is in the relationship configuration. If you want to configure your one to many relation without using a FK, you could do this:
modelBuilder.Entity<Employee>()
.HasMany(e => e.Employees)
.WithOptional(e => e.Supervisor);
Now if you want to use a FK property, then add this property to your model class:
public class Employee
{
//...
public int SupervisorId { get; set; }
}
And map your relationship this way:
modelBuilder.Entity<Employee>()
.HasMany(e => e.Employees)
.WithOptional(e => e.Supervisor)
.HasForeignKey(e => e.SupervisorId);
To resolve your issue related with ReportTo and Position properties,I think you should handle that logic in your code. If you want to know if an Employee is a supervisor based on the count of Employees property, you could use a NotMapped property:
public class Employee
{
[NotMapped]
public bool IsSupervisor
{
get
{
return Employess.Count>0
}
}
}
You can do the same using Fluent Api:
modelBuilder.Entity<Employee>().Ignore(e => e.IsSupervisor);
PS: Remember initialize Employees in your class'constructor.
The error you get is because it is trying to map a PK of int type to a FK of string type. User int for all of your key fields.
Then, you need to declare your OnModelBuilding like this:
modelBuilder.Entity<Employee>()
.HasOptional(e => e.Supervisor)
.WithMany()
.HasForeignKey(s => s.ReportsTo);
To get something like IsSupervisor() you can take advantage of partial classes. Create another class file which is a public partial class Employee (and modify your original one to be partial), then in your new file you will add a property that does whatever you want, and decorate it with [NotMapped] attribute. Yours will probably look something like public bool IsSupervisor {get { return (Employees == null) ? false : true; } set {} } The new partial class is where you can do whatever you want for the POCO without changing the EF class (make sure you use [NotMapped] though).

EF6 - TPH foreign key mapping in derived classes using base class property

I am using Entity Framework 6.0.2 with an existing database in which tags are stored in a single table that looks like this:
Id: int, primary key
TagType: string, determine the type of tag, either "usertag" or "movietag"
ItemId: int, contains the Id of the item to which is referred (either a User Id or a Movie Id)
The following classes describe this situation:
public class User
{
public int Id { get; set; }
}
public class Movie
{
public int Id { get; set; }
}
public abstract class Tag
{
public int Id { get; set; }
public int ItemId { get; set; }
}
public class UserTag : Tag
{
public virtual User User { get; set; }
}
public class MovieTag : Tag
{
public virtual Movie Movie { get; set; }
}
As you can see my derived classes have navigation properties, which are backed by the value of the ItemId property in the base class. My mapping is as follows:
public class Context : DbContext
{
public DbSet<Tag> Tags { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Tag>()
.Map<UserTag>(m => m.Requires("TagType").HasValue("usertag"))
.Map<MovieTag>(m => m.Requires("TagType").HasValue("movietag"));
modelBuilder.Entity<UserTag>()
.HasRequired(m => m.User).WithMany().HasForeignKey(m => m.ItemId);
modelBuilder.Entity<MovieTag>()
.HasRequired(m => m.Movie).WithMany().HasForeignKey(m => m.ItemId);
}
}
Now when I try to use this mapping using the following code, I get an exception:
using System.Data.Entity;
class Program
{
static void Main()
{
using (var db = new Context())
{
db.Database.Delete();
db.Database.Initialize(false);
}
}
}
The exception that is thrown is:
Unhandled Exception: System.InvalidOperationException: The foreign key component 'ItemId' is not a declared property on type 'UserTag'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property
Yes the ItemId property is not declared on the type UserTag, but it is inherited from the base Tag class. To me it seems that this mapping should be possible. Is this a bug or a restriction in Entity Framework 6?
It is a restriction. EF is quite tightly bound to the way how relational database works. What you are trying to do in terms of the database is to put two foreign key constraints on single ItemId column. The foreign constraint in database is not conditional so the record will always use both constraints no matter of the tag type. That is not what you want because such definition will always require both user and movie with specific Id to exist for every single tag.
Think about it in different way. If it works the way how you are trying to define it there would be no reason why to have User and Movie navigation properties in child entities - it would be enough to have single navigation property in parent. The fact that you have to define them in child entities because they are different for each of them also means you need to have two different foreign keys.
You need to have separate UserId and MovieId in their specific tags.

Many to many relationship identity error

I'm working on a mvc4 app with ef5 codefirst and I cannot solve this error:
The member with identity 'xxxx' does not exist in the metadata collection.
Update:
I saw that I used two different contexts (the navigation object was called thorugh a repository that creates a different DbContext), probably this is a problem. I changed that, but now I get a new error:
Invalid column name 'Brewery_BreweryId'.
In the IntelliTrace I saw that ef tries to
select ..., Brewery_BreweryId from UserProfiles
This column is not present and shouldn't be present, I want a many to many, not a one-to-many.
I think that is something related to a many to many relation.
this is an example of my code
internal class BreweryConfiguration : EntityTypeConfiguration<Brewery>
{
public BreweryConfiguration()
{
// PK
HasKey(e => e.BreweryId);
// FK
HasMany(e => e.UserProfiles)
.WithMany()
.Map(m =>
{
m.MapLeftKey("BreweryId");
m.MapRightKey("UserId");
m.ToTable("BreweryUserProfiles");
});
namespace Project2.DAL.Entities
{
[Table("Breweries")]
public class Brewery : ABrewery
{
public int BreweryId { get; set; }
public ICollection<UserProfile> UserProfiles { get; set; }
}
}
namespace Project1.DAL.Entities
{
[Table("UserProfiles")]
public class UserProfile : IUserProfile
{
[Key]
public int UserId { get; set; }
...
}
}
c.MapLeftKey("ClassB_ID");
c.MapRightKey("ClassA_ID");
should be
c.MapLeftKey("ClassA_ID");
c.MapRightKey("ClassB_ID");
Edit:
You need to define the PK of the ClassB in the configuration as well. In the way you implemented, you may add another derived Configuration for ClassB.

Categories

Resources