如何分配和传递变量

时间:2019-02-15 07:35:23

标签: c#

我有以下代码,它们从用户表单中提取数据,如下所示,并写入数据库:

public class Member_Extended : Member
{

    public string Source { get; set; }
    public string Other { get; set; }
}
[WebMethod(EnableSession = true)]

public Dictionary<string, object> CreateMemeber(Member_Extended member) {
    var original = member != null ? MemberExtended_Member.Compile().Invoke(member) : null;
    Dictionary<string, object> data = new Dictionary<string, object>();
    context.Connection.Open();
    int success = 0;
    using (DbTransaction transaction = context.Connection.BeginTransaction()) {
        try {

            original.isPending = true;
            original.ExpiresOn = DateTime.Now.AddDays(14);

            context.AddToMember(original);
            success = context.SaveChanges();

            if (success == 1) {
                transaction.Commit();


                CacheManager.MemberData =original;  // how to pass original into member
                 MemeberExtendedRepository.Insert(member)
            }
            Utils.ServiceSuccessResultHandler(ref data, "Login," + original.ID, 1);
        } catch (Exception ex) {
            transaction.Rollback();
            Utils.ServiceErrorResultHandler(ex, ref data);
        }

    }

    return data;
}

public class MemeberExtendedRepository
{

    public static bool Insert (Member_Extended member)
    {
        var command = new StringBuilder();
        command.AppendFormat(string.Format("UPDATE Table SET [Source]='{0}',[Other]='{1}' WHERE  ID={2} ", member.Source, member.Other, member.ID));
        return ExecuteCommand(command.ToString());
    }
}

您可以在v ar original = member != null ? MemberExtended_Member.Compile().Invoke(member) : null;这行看到,我已将成员分配给原始成员。因为原始文件包含“ ID”参数值示例1234。 当我调用此方法MemeberExtendedRepository.Insert(member)时,我希望传递原始变量的ID值,当前它返回ID为0。

当我尝试以下操作时  MemeberExtendedRepository.Insert(original);表示无法将成员转换为Member_Extended。

我该如何实现?

0 个答案:

没有答案