Cannot convert type 'TestLinq.UserData' to T'estLinq.userData' - entity-framework

Still working out concepts in EF6 (learning) This is not production code; it's only for learning...
I'm experimenting with migrating to EF6 from direct SQL ODBC calls and trying to duplicate features of methods I have currently in my code.
Consider this code:
public class user
{
[Key]
public int pkID;
public string ForeignID;
public string UserName;
public virtual List<userData> UserDatas { get; set; }
}
public class userData
{
[Key]
public int pkID;
public int fkUserID;
public string ColumnName;
public string ColumnValue;
public bool IsOverrode;
public string OverrideValue;
}
class Program
{
static TestDataEntities db = new TestDataEntities();
static void Main(string[] args)
{
var record = db.Users.Select(x => new { x.UserName, x.pkID }).FirstOrDefault();
Console.WriteLine(record);
var record2 = db.Users.Include(x => x.UserDatas).FirstOrDefault();
record2.UserName = "JohnTestNew";
db.SaveChanges();
var ud = record2.UserDatas.Where(x => x.ColumnName.ToUpper().Contains("FIRSTNAME")).FirstOrDefault();
foreach (var item in record2.UserDatas)
Console.WriteLine(item.ColumnName);
Console.ReadLine();
}
static userData TestReturn()
{
var record2 = db.Users.Include(x => x.UserDatas).FirstOrDefault();
var ud = record2.UserDatas.Where(x => x.ColumnName.ToUpper().Contains("FIRSTNAME")).FirstOrDefault();
return (userData)ud;
}
I'm getting a compile error on the last line:
return (userData)ud;
'Cannot convert type 'TestLinq.UserData' to 'TestLinq.userData'
I kind of understand what is going on, but I thought I could cast the class on the return. The UserData is contained in the returned query, the userData is the raw class of what UserData is.
Is there a way to return this object using the base class?

Related

EF Core 6: How to implement controller that returns joined data?

I have a database with several different, but related tables:
class AccountInfo {
string id;
string name;
string email;
}
class ExtraInfo {
string id;
string proxy;
}
class UserInfo {
AccountInfo account;
ExtraInfo extra;
}
public class MyDbContext : DbContext {
public DbSet<AccountInfo> AccountInfo { get; set; }
public DbSet<ExtraInfo> ExtraInfo { get; set; }
}
public class ExtraInfoController : ODataController
{
private readonly DS2DbContext _context;
private readonly ILogger<UserInfoController> _logger;
public ExtraInfoController(DS2DbContext context) {
_context = context;
}
[EnableQuery(PageSize = 15)]
public IQueryable<UserInfo> Get() {
IQueryable<UserInfo> query =
from a in _context.AccountInfo
from x in _context.ExtraInfo
where (a.id == x.id)
select new UserInfo() { account = a, extra = x };
return query;
}
The query in ExtraInfoController.Get() works, but the result cannot be transferred back to the calling code, because the DbSet is declared for type ExtraInfo and I guess because of the way Blazor mangles all the pseudo code passed to it into working code, so it expects the return type to be IQueryable and not IQeryable.
I am new to EF core, so I don't know how to create a controller that is not directly related to a db table that would do the desired join and return an IQueryable without there being a UserInfo table in the db.

How to get a property name of a given type strongly typed revisited?

How can I simplify the code below to avoid to pass the object for type inference on the generic method?
using System;
using System.Linq.Expressions;
namespace lambda
{
class Program
{
static void Main(string[] args)
{
var area = new Area { Name = "New Area" };
var propertyName = area.GetPropertyName(area, a => a.Name); // propertyName is COMPILE time checked
Console.WriteLine(propertyName);
}
}
public class Area
{
public int Id;
public string Name { get; set; }
}
public static class Extension
{
public static string GetPropertyName<T>(this Area entity, T e, Expression<Func<T, object>> path) // T e for type inference
{
var member = path.Body as MemberExpression;
if (member == null) throw new ArgumentException();
return member.Member.Name;
}
}
}
I mean instead of calling the extension method with area.GetPropertyName(area, a => a.Name)
just do a call like this area.GetPropertyName(a => a.Name), avoid to pass there area object just for type inference
I guess that I can’t do unless I refactor the signature of the method to GetPropertyName(this IEntity entity, Expression> path)
But in that case will be less obvius want I want at code writing time since I will need to specify the type on every call
I mean area.GetPropertyName( a => a.Name) seems to bel for me less clear writing code than writing area.GetPropertyName(area, a => a.Name)
The example code below works fine with asked requirements, no need to pass the object itself for type inference when calling the extension method
I used a base class and an interface that for my case works fine for all my domain class.
See code below
namespace UnitTestProject
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq.Expressions;
public interface IEntity<T>
{
}
public abstract class Entity<T> : IEntity<T> where T : class
{
}
public class Area : Entity<Area>
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Created { get; set; }
public bool Closed { get; set; }
public string Description { get; set; }
}
public static class EntityExtension
{
public static string GetPropertyName<T>(this IEntity<T> entity, Expression<Func<T, object>> expr) // T e for type inference
{
var unary = expr.Body as UnaryExpression;
var member = expr.Body as MemberExpression;
return member != null ? member.Member.Name : (unary != null ? ((MemberExpression)unary.Operand).Member.Name : String.Empty);
}
}
[TestClass]
public class UnitTest
{
[TestMethod]
public void GetPropertyName_Tests()
{
var area = new Area();
var x = area.GetPropertyName(a => a.Id);
var y = area.GetPropertyName(a => a.Name);
var v = area.GetPropertyName(a => a.Created);
var w = area.GetPropertyName(a => a.Closed);
var z = area.GetPropertyName(a => a.Description);
Assert.AreEqual(x, "Id");
Assert.AreEqual(y, "Name");
Assert.AreEqual(v, "Created");
Assert.AreEqual(w, "Closed");
Assert.AreEqual(z, "Description");
}
}
}

Entity framework Generic query in Nongeneric Property

In Entity framework I have objectsets like
public partial class Building
{
public int BuildingID { get; set; }
public string BuildingName { get; set; }
}
public partial class Town
{
public int TownID { get; set; }
public string TownName { get; set; }
}
I want to create a generic query like
T.OrderBy(o=>o.Id).Skip(maxDispItem * (page - 1)).Take(maxDispItem).ToList();
T is generic class can be Building or Town but problem is BuildingId and TownId has different name.I don't want to change their name as Id and create interface IIdentity.
Maybe you could try something like this:
var query = (typeof(T) == typeof(Building) ?
context.Buildings.Select(b => new { Id = b.BuildingId, Name = b.BuildingName }) :
context.Towns.Select(t => new { Id = t.TownId, Name = b.TownName }))
.OrderBy(o => o.Id)...
Not tested but that's worth a test...
You can create generic method which find a field decorated with KeyAttribute, and then performs sorting by found key field. I have tested your model, works perfectly. Look at code snippet.
DbContext:
using System.Collections.Generic;
using System.Data.Entity;
namespace ConsoleApplication28.Entities
{
public class AppDbContext : DbContext
{
public AppDbContext()
{
Database.Connection.ConnectionString = #"Data Source=NOTEBOOK-PC;Initial Catalog=StackOverflowTest;Integrated Security=True";
Database.SetInitializer(new AppDbInitializer());
}
public DbSet<Town> Towns { get; set; }
public DbSet<Building> Buildings { get; set; }
}
public class AppDbInitializer : DropCreateDatabaseIfModelChanges<AppDbContext>
{
protected override void Seed(AppDbContext context)
{
context.Buildings.AddRange(new List<Building>
{
new Building {BuildingName = "Building1"},
new Building {BuildingName = "Building2"},
});
context.Towns.AddRange(new List<Town>
{
new Town {TownName = "Town1"},
new Town {TownName = "Town2"},
});
context.SaveChanges();
base.Seed(context);
}
}
}
Building
using System.ComponentModel.DataAnnotations;
namespace ConsoleApplication28.Entities
{
public class Building
{
[Key]
public int BuildingID { get; set; }
public string BuildingName { get; set; }
}
}
Town
using System.ComponentModel.DataAnnotations;
namespace ConsoleApplication28.Entities
{
public class Town
{
[Key]
public int TownID { get; set; }
public string TownName { get; set; }
}
}
Program
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using ConsoleApplication28.Entities;
using System.ComponentModel.DataAnnotations;
namespace ConsoleApplication28
{
class Program
{
static void Main(string[] args)
{
const int maxDispItem = 10;
const int page = 1;
var db = new AppDbContext();
var towns = db.Towns.OrderByKey().Skip(maxDispItem * (page - 1)).Take(maxDispItem).ToList();
var buildings = db.Buildings.OrderByKey().Skip(maxDispItem * (page - 1)).Take(maxDispItem).ToList();
}
}
public static class Extensions
{
/// <summary>
/// Sorts the elements of a sequence in ascending order according to a key specified using KeyAttribute
/// </summary>
public static IOrderedQueryable<T> OrderByKey<T>(this IQueryable<T> source, bool isAsc = true)
{
var type = typeof(T);
var keyProperty = type.GetProperties().Single(x => x.GetCustomAttributes(typeof(KeyAttribute)).Any());
return source.OrderBy(keyProperty.Name, isAsc);
}
#region COPIED FROM THERE http://stackoverflow.com/questions/41244/dynamic-linq-orderby-on-ienumerablet
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property, bool isAsc)
{
return isAsc ? source.OrderBy(property) : source.OrderByDescending(property);
}
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderBy");
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderByDescending");
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenBy");
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenByDescending");
}
static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
{
string[] props = property.Split('.');
Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
foreach (string prop in props)
{
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
object result = typeof(Queryable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object[] { source, lambda });
return (IOrderedQueryable<T>)result;
}
#endregion
}
}

Generate Guid on Serverside Entity Framework 5?

I am coming from a nhibernate background and I am wondering how can I generate the Guid automatically on the serer side and not make a round trip to make it on the database side?
In fluent nhibernate it is simple just
Id(x => x.Id).GeneratedBy.GuidComb();
If you want to generate the key on the server, simply do this in code:
public class TestObject
{
public TestObject()
{
Id = Guid.NewGuid();
}
public Guid Id { get; set; }
}
If you want the database to generate the key, then use the DatabaseGenerated attribute:
public class TestObject
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
}
If you're after using sequential GUIDs, then there's no easy answer at the moment. Some examples which get you along the right road:
Generating IDs in the SaveChanges method
Calling your own NewGuid method
Use a non-EF method to change the default value for the identity field from NEWID() to NEWSEQUENTIALID()
This code does what you need:
using System;
using System.Runtime.InteropServices;
public static class SequentialGuidProvider
{
[DllImport("rpcrt4.dll", SetLastError = true)]
private static extern int UuidCreateSequential(out Guid guid);
private static Guid CreateGuid()
{
Guid guid;
int result = UuidCreateSequential(out guid);
if (result == 0)
return guid;
else
return Guid.NewGuid();
}
public static Guid GuidComb(this Nullable<Guid> guid)
{
if (!guid.HasValue) guid = SequentialGuidProvider.CreateGuid();
return guid.Value;
}
}
Test class:
public class TestObject
{
public TestObject()
{
}
private Nullable<Guid> _guid = null;
public Guid Id
{
get
{
_guid = _guid.GuidComb();
return _guid.Value();
}
set
{
_guid = value;
}
}
}
Test code:
static void Main(string[] args)
{
TestObject testObject1 = new TestObject();
TestObject testObject2 = new TestObject();
TestObject testObject3 = new TestObject();
//simulate EF setting the Id
testObject3.Id = new Guid("ef2bb608-b3c4-11e2-8d9e-00262df6f594");
//same object same id
bool test1 = testObject1.Id == testObject1.Id;
//different object different id
bool test2 = testObject1.Id != testObject2.Id;
//EF loaded object has the expected id
bool test3 = testObject3.Id.Equals(new Guid("ef2bb608-b3c4-11e2-8d9e-00262df6f594"));
}
As of EF 6.1.3, database defaults [newsequentialid() or newid()] may be important when using GUID's as PK's
See entity framework use a guid as the primary key.

WCF Data Service based on EF5 Model; how to add a custom type

I am trying to build a WCF Data Service with a ServiceMethod returning a custom type.
This type is used as a container to transmit multiple data collection at once. I am not able to define this type as entity or complex type.
public class BrfPackageDataContainer {
public ICollection<BrfFlight> Flights {
get;
set;
}
public ICollection<BrfFlight_Info> Flight_Infos {
get;
set;
}
public ICollection<BrfInfo> Infos {
get;
set;
}
public BrfPackageDataContainer() {
this.Flights = new List<BrfFlight>();
this.Flight_Infos = new List<BrfFlight_Info>();
this.Infos = new List<BrfInfo>();
}
}
This is my ServiceMethod declaration:
[WebGet]
[SingleResult]
public FlightInfoEntities.BrfPackageDataContainer GetBrfPackage () {
var brfPackageDataContainer = new FlightInfoEntities.BrfPackageDataContainer();
brfPackageDataContainer.Demodata();
return brfPackageDataContainer;
}
I got this running when using an empty dummy DataService as data source for the service class definition. But when I use my Entity Framework Model as data source the service refuse to start because of the missing metadata for the custom type.
My question is:
How can I use an EF Model as data source AND still use my custom type as a return value for my method.
Problem solved with a workaround:
I added 3 complex types to my modell, matching the data structure of each individual result set.
Furthermore I added a container class outside the data context which uses the complex types to hold the data in one object.
I extended the context class with a custom method to handle the stored procedure call and mapping the results to the appropriate complex types.ObjectContext.Translate helps a lot...
The WCF Data Service class is instantiated with a dummy DataContext. This enables metadata creation for my custom data container class which now can be used as return type of a custom WCF Data Service Method.
The data context is instantiated when the method is called.
Data container class` public class BrfPackageDataContainer {
public Guid TransactionId {
get;
set;
}
public List<BrfFlight> Flights {
get;
set;
}
public List<BrfFlight_Info> Flight_Infos {
get;
set;
}
public List<BrfInfo> Infos {
get;
set;
}
public BrfPackageDataContainer () {
this.Flights = new List<BrfFlight>();
this.Flight_Infos = new List<BrfFlight_Info>();
this.Infos = new List<BrfInfo>();
}
}`
context extension:
public partial class FlightInfoEntities
{
public virtual BrfPackageDataContainer GetBrfPackage(int? crewId, string operatorCode, string departure, int? flightId, DateTime? stdRangeStart,
DateTime? stdRangeEnd, string requestingApplication, string requestingComputerName,
string requestingACReg, ref Guid transactionId, int? specificInfoTypeId, byte? levelOfDetail,
bool? skipLog) {
using (DbCommand command = this.Database.Connection.CreateCommand()) {
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "[dbo].[GetBrfPackage]";
...
var dataContainer = new BrfPackageDataContainer();
try {
this.Database.Connection.Open();
using (DbDataReader reader = command.ExecuteReader()) {
dataContainer.Flights = ((IObjectContextAdapter)this).ObjectContext.Translate<BrfFlight>(reader).ToList();
reader.NextResult();
dataContainer.Flight_Infos = ((IObjectContextAdapter)this).ObjectContext.Translate<BrfFlight_Info>(reader).ToList();
reader.NextResult();
dataContainer.Infos = ((IObjectContextAdapter)this).ObjectContext.Translate<BrfInfo>(reader).ToList();
}
return dataContainer;
} catch (Exception ex) {
throw ex;
}
}
WCF Data Service Method:
[WebGet]
[SingleResult]
public BrfPackageDataContainer GetBrfPackage () {
using (var brfCtx = new FlightInfoEntities()) {
Guid transactionId = new Guid();
var brfPackageDataContainer = brfCtx.GetBrfPackage(null,"4T",null,null,null,null,"test",Environment.MachineName,null,ref transactionId,null,3,false);
return brfPackageDataContainer;
}
}