这是我自动生成的类Customer的开头:
namespace Winpro
{
using System;
using System.Collections.Generic;
public partial class Customer
{
public Customer()
{
this.Blocked = false;
this.Code = "#00000";
this.RuleId = 1;
this.LocationId = 1;
this.Contacts = new ObservableListSource<Contact>();
}
public int Id { get; set; }
public string Name { get; set;
public System.DateTime Added { get; set; }
...
为什么我不能以这种方式扩展课程。
namespace Winpro
{
public partial class Customer
{
public Customer()
{
this.Added = DateTime.Now;
}
查找在单独的类中设置默认值或覆盖SaveChanges()方法的简单示例。
由于
答案 0 :(得分:1)
partial class是一个分为多个文件的类。它仍然是一个单独的类,并且您不能拥有两个具有相同签名的构造函数。
您可以尝试:
this
Added
属性
namespace Winpro
{
public partial class Customer
{
public Customer(DateTime parameterAdded)
: this() //call the default constructor
{
this.Added = parameterAdded; //DateTime.Now;
}
答案 1 :(得分:0)
在生成的类
中public partial class Customer
{
public Customer()
{
this.Blocked = false;
this.Code = "#00000";
this.RuleId = 1;
this.LocationId = 1;
this.Contacts = new ObservableListSource<Contact>();
AdditionnalInitialisation();
}
partial void AdditionnalInitialisation();
在你的扩展程序中:
public partial class Customer
{
partial void AdditionnalInitialisation()
{
this.Added = DateTime.Now;
}
}