C# 继承、基类、派生类
我又白手起家了。。我想知道下面的代码是否可以用于编写和查询数据库。而不是获取和设置个人文件,如城市、州。我希望容器类能够做到这一点。。当我查询一个人时,获取并发送contactinfo对象以及其他人数据到主程序C# 继承、基类、派生类,c#,database,inheritance,C#,Database,Inheritance,我又白手起家了。。我想知道下面的代码是否可以用于编写和查询数据库。而不是获取和设置个人文件,如城市、州。我希望容器类能够做到这一点。。当我查询一个人时,获取并发送contactinfo对象以及其他人数据到主程序 namespace ConsoleApplication1 { using System; using System.Collections.Generic; public partial class PERSON { public PE
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
public partial class PERSON
{
public PERSON()
{
this.MANAGERs = new HashSet<MANAGER>();
this.TRANSACTIONLISTs = new HashSet<TRANSACTIONLIST>();
}
public PERSON(string fName, string lName, ContactInfo contactInfo)
{
F_NAME = fName;
L_NAME = lName;
ContactInfo = contactInfo;
this.MANAGERs = new HashSet<MANAGER>();
this.TRANSACTIONLISTs = new HashSet<TRANSACTIONLIST>();
}
public ContactInfo ContactInfo {
get
{
ContactInfo.Phone = PHONE;
ContactInfo.Email = EMAIL;
ContactInfo.Address = new Address() { Street = ADDRESS, City = CITY, State = STATE, Zip = ZIP };
return ContactInfo;
}
set
{
PHONE = ContactInfo.Phone;
EMAIL = ContactInfo.Email;
ADDRESS = ContactInfo.Address.Street;
CITY =ContactInfo.Address.City;
STATE = ContactInfo.Address.State;
ZIP = ContactInfo.Address.State;
}
}
public string F_NAME { get; set; }
public string L_NAME { get; set; }
public string ADDRESS { get; set; }
public string CITY { get; set; }
public string STATE { get; set; }
public string ZIP { get; set; }
public string PHONE { get; set; }
public string EMAIL { get; set; }
public decimal ID { get; set; }
public string CURRENTMEMBER { get; set; }
public Nullable<decimal> TOTALDUE { get; set; }
public string ISMEMBER { get; set; }
public string PASSWORD { get; set; }
public virtual ICollection<MANAGER> MANAGERs { get; set; }
public virtual ICollection<TRANSACTIONLIST> TRANSACTIONLISTs { get; set; }
}
}
命名空间控制台应用程序1
{
使用制度;
使用System.Collections.Generic;
公共部分阶级人士
{
公众人士()
{
this.MANAGERs=new HashSet();
this.TransactionList=new HashSet();
}
公众人物(字符串fName、字符串lName、ContactInfo ContactInfo)
{
F_NAME=fName;
L_NAME=lName;
ContactInfo=ContactInfo;
this.MANAGERs=new HashSet();
this.TransactionList=new HashSet();
}
公共联系人信息联系人信息{
收到
{
ContactInfo.Phone=电话;
ContactInfo.Email=电子邮件;
ContactInfo.Address=newaddress(){Street=Address,City=City,State=State,Zip=Zip};
返回联系人信息;
}
设置
{
电话=ContactInfo.PHONE;
EMAIL=ContactInfo.EMAIL;
地址=ContactInfo.ADDRESS.Street;
城市=ContactInfo.Address.CITY;
状态=ContactInfo.Address.STATE;
ZIP=ContactInfo.Address.State;
}
}
公共字符串F_NAME{get;set;}
公共字符串L_NAME{get;set;}
公共字符串地址{get;set;}
公共字符串CITY{get;set;}
公共字符串状态{get;set;}
公共字符串ZIP{get;set;}
公用字符串电话{get;set;}
公共字符串电子邮件{get;set;}
公共十进制ID{get;set;}
公共字符串CURRENTMEMBER{get;set;}
公共可空TOTALDUE{get;set;}
公共字符串ISMEMBER{get;set;}
公共字符串密码{get;set;}
公共虚拟ICollection管理器{get;set;}
公共虚拟ICollection事务列表{get;set;}
}
}
你的设计让我有点困惑。您从PersonController继承PERSON,并在PersonController(父类)内直接使用PERSON(子类)。
如果我是您,我将创建一个从PERSON继承的成员类(我的实体类)和
包含与PERSON相关的方法的控制器类(T必须是PERSON或子类)。我必须说,您当前的设计非常令人困惑,即使您设法使其工作,它也会让人困惑。您应该从遵循iDesign C#命名约定开始 我会从头开始,从
Person
类开始,重新思考它的属性。这些属性中的哪一个会组成一个好的逻辑信息组,使它们也成为类
可能会出现如下情况
public class Person
{
public Person()
{
}
public Person(string fName, string lName, ContactInfo contactInfo)
{
FirstName = fName;
LastName = lName;
ContactInfo = contactInfo;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public ContactInfo ContactInfo { get; set; }
}
其中,ContactInfo
由电话号码、电子邮件和地址组成
public class ContactInfo
{
public string Phone { get; set; }
public string Email { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public int Zip { get; set; }
}
现在,您的成员将是个人,但附加了其他属性。所以它可能看起来像下面这样
public class Member : Person
{
public Member(string fName, string lName, ContactInfo contactInfo)
: base(fName, lName, contactInfo)
{
Id = 0;
CurrentMember = "Y";
TotalDue = 0;
IsMember = "Y";
Password = "Default";
Managers = new HashSet<Manager>();
Providers = new HashSet<Provider>();
Transactionlists = new HashSet<Transactionlist>();
}
public decimal Id { get; set; }
public string CurrentMember { get; set; }
public decimal? TotalDue { get; set; }
public string IsMember { get; set; }
public string Password { get; set; }
public ICollection<Manager> Managers { get; set; }
public ICollection<Provider> Providers { get; set; }
public ICollection<Transactionlist> Transactionlists { get; set; }
}
和派生的MemberController
public class MemberController : ControllerBase<Member>
{
public override void InsertOrUpdate(Member obj)
{
if (obj == null)
throw new ArgumentException("Member");
using (var context = new ChocDB())
{
var state = obj.Id == 0 ? EntityState.Added : EntityState.Modified;
if (state == EntityState.Added)
context.People.Add(obj);
context.SaveChanges();
}
}
}
public类成员控制器:ControllerBase
{
公共覆盖无效插入更新(成员obj)
{
if(obj==null)
抛出新的ArgumentException(“成员”);
使用(var context=new ChocDB())
{
var state=obj.Id==0?EntityState.Added:EntityState.Modified;
if(state==EntityState.Added)
context.People.Add(obj);
SaveChanges();
}
}
}
这有什么意义吗 会员和个人是两张不同的桌子吗?只有一张桌子。。我需要使用一个不同的表来标识管理器和提供者,但这是另一个挑战。因为成员类继承自Person类,所以所有成员实例都是Person实例。ID是Person类的属性。你能分享一下你面临的问题吗?你所说的“有问题。身份证被认为是person类的一部分”是什么意思?显然,Person
具有ID
属性。如果您遇到错误,请特别说明您遇到的错误以及其他详细信息。发布一个好的、简洁的、完整的代码示例。See Id希望能够更改personcontroller,以便它能够在数据库中的所有实体中工作。在经历了一段时间的颠簸之后,这非常有意义。我正在尝试使用泛型、继承、数据库等来解决这个问题。。谢谢您的帮助。大量修改原始问题并不酷。现在这个答案完全断章取义,因此,对任何其他人都毫无用处。
public class MemberController : ControllerBase<Member>
{
public override void InsertOrUpdate(Member obj)
{
if (obj == null)
throw new ArgumentException("Member");
using (var context = new ChocDB())
{
var state = obj.Id == 0 ? EntityState.Added : EntityState.Modified;
if (state == EntityState.Added)
context.People.Add(obj);
context.SaveChanges();
}
}
}