我有以下Method
,它根据作为参数传递的Primary Key
返回记录:
class Program
{
public static string GetRecord(Int64 pk)
{
using (var entityDataModel = new EntityDataModel())
{
var record = entityDataModel.Catalog.Find(pk);
return record.VehicleMake;
}
}
static void Main(string[] args)
{
Console.WriteLine(GetRecord(7341367950));
Console.ReadKey();
}
}
而不是上面的代码,我需要Method来返回一个公开记录的对象,这取决于我通过调用者传递的主键作为参数:我可以在这里使用什么数据类型?
像这样:(示例)
class Program
{
public static SomeDataType GetRecord(Int64 pk)
{
using (var entityDataModel = new EntityDataModel())
{
//Return record based on PK passed
return entityDataModel.Catalog.Find(pk);
}
}
static void Main(string[] args)
{
// Call the method here and return the data object
var record = GetRecord(7341367950);
Console.WriteLine(record.VehicleMake);
//Or like this:
Console.WriteLine(GetRecord(7341367950).VehicleMake);
Console.ReadKey();
}
}
更新:这是模型:
public partial class EntityDataModel : DbContext
{
public EntityDataModel()
: base("name=EntityDataModel")
{
}
public virtual DbSet<Catalog> Catalog { get; set; }
public virtual DbSet<Model> Model { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Catalog>()
.Property(e => e.VehicleIdentificationNumber)
.IsFixedLength();
}
}
答案 0 :(得分:0)
答案:
我只是使用实体,在本例中是实体模型目录,它代表数据库中的一个表:
public static Catalog GetRecord(Int64 pk)
{
using (EntityDataModel entityDataModel = new EntityDataModel())
{
return entityDataModel.Catalog.Find(pk);
}
}
static void Main(string[] args)
{
Catalog record = GetRecord(7341367950);
Console.WriteLine(record.VehicleMake);
Console.ReadKey();
}