मेरे पास इकाई फ्रेमवर्क कोड प्रथम दृष्टिकोण का उपयोग करके बनाई गई निम्न तालिका है।इकाई फ्रेमवर्क: टेबल से डिस्कमिनेटर कॉलम से कैसे बचें?
- मैं सी # कोड को कैसे संशोधित करूं ताकि अवांछित डिस्कमिनेटर कॉलम डेटाबेस में नहीं बनाया जा सके? क्या यह हासिल करने के लिए कोई विशेषता है?
- "भुगतान_ भुगतान आईडी" के बजाय विदेशी कुंजी कॉलम नाम को "भुगतान आईडी" के रूप में कैसे बनाया जाए? क्या यह हासिल करने के लिए कोई विशेषता है?
नोट: EntityFramework.dll के लिए रनटाइम संस्करण है v4.0.30XXX
कोड
public abstract class PaymentComponent
{
public int PaymentComponentID { get; set; }
public int MyValue { get; set; }
public string MyType { get; set; }
public abstract int GetEffectiveValue();
}
public partial class GiftCouponPayment : PaymentComponent
{
public override int GetEffectiveValue()
{
if (MyValue < 2000)
{
return 0;
}
return MyValue;
}
}
public partial class ClubCardPayment : PaymentComponent
{
public override int GetEffectiveValue()
{
return MyValue;
}
}
public partial class Payment
{
public int PaymentID { get; set; }
public List<PaymentComponent> PaymentComponents { get; set; }
public DateTime PayedTime { get; set; }
}
//System.Data.Entity.DbContext is from EntityFramework.dll
public class NerdDinners : System.Data.Entity.DbContext
{
public NerdDinners(string connString): base(connString)
{
}
protected override void OnModelCreating(DbModelBuilder modelbuilder)
{
modelbuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
public DbSet<GiftCouponPayment> GiftCouponPayments { get; set; }
public DbSet<ClubCardPayment> ClubCardPayments { get; set; }
public DbSet<Payment> Payments { get; set; }
}
ग्राहक
static void Main(string[] args)
{
string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30";
using (var db = new NerdDinners(connectionstring))
{
GiftCouponPayment giftCouponPayment = new GiftCouponPayment();
giftCouponPayment.MyValue=250;
giftCouponPayment.MyType = "GiftCouponPayment";
ClubCardPayment clubCardPayment = new ClubCardPayment();
clubCardPayment.MyValue = 5000;
clubCardPayment.MyType = "ClubCardPayment";
List<PaymentComponent> comps = new List<PaymentComponent>();
comps.Add(giftCouponPayment);
comps.Add(clubCardPayment);
var payment = new Payment { PaymentComponents = comps, PayedTime=DateTime.Now };
db.Payments.Add(payment);
int recordsAffected = db.SaveChanges();
}
}
यदि आप सम्मेलनों को ओवरराइड करते हैं तो अन्य डेवलपर्स आपकी स्कीमा को समझने में संघर्ष करेंगे। सम्मेलनों को सीखना और उनका उपयोग करना अधिक फायदेमंद है, यानी आपको अन्य डेवलपर्स कोड या डिफ़ॉल्ट जेनरेट कोड पढ़ने पर प्रश्न पूछना नहीं होगा और आपको अतिरिक्त कोड भी लिखना पड़ेगा। – user3285954