ASP.NET和编程方面的新手。我有两个模型,两个API控制器,两个存储库。如何在将数据附加到第一个模型时将数据发布到第二个模型(我是通过ID猜测的。)我是否可能需要View模型?还阅读一些关于工作单元的内容。也许两者都没有必要?下面是一些代码。谢谢!
Record.cs
public static void buildTree(String treeFile){
//temp string array to hold the input as 3 seperate Strings, seperated by ;
String[] temp= new String[3];
int i=0;
//will split the string into its 3 parts
for (String newTemp: treeFile.split(";")) {
temp[i]=newTemp;
i++;
}
//can hold up to 100 char, each describing a path
char[] pathArray= new char[100];
int pathCursor=0;
i=0;
//once again split the first part of the string, which holds the path, into an array of chars,
//parse each String into a char and then set equal to in array in array
for(String newTemp: temp[0].split("-")){
char c=newTemp.charAt(0);
pathArray[i]=c;
i++;
pathCursor++;
}
for(int j=0; j<3;j++){
System.out.println(temp[j]);
}
for(int j=0; j<pathCursor;j++){
System.out.println(pathArray[j]);
}
}
}
Cars.cs
namespace Train.Models {
public class Record {
public int Id { get; set; }
public int Quantity { get; set; }
public DateTime DateCreated { get; set; }
public bool IsActive { get; set; }
public string UserId { get; set; }
public virtual ICollection<Cars> Cars { get; set; }
}
}
记录存储库
namespace Train.Models {
public class Cars {
public int Id { get; set; }
public string EmptyOrLoaded { get; set; }
public string CarType { get; set; }
//Hopper, flatbed, tank, gondola, etc.
public string ShippedBy { get; set; }
//UP(Union Pacific) or BNSF
public string RailcarNumber { get; set; }
//public virtual ApplicationUser ApplicationUser { get; set; }
public string UserId { get; set; }
public string RecordId { get; set; }
public virtual Record Record { get; set; }
}
EFRepository(汽车)
public void SaveRecord(Record recordToSave) {
if (recordToSave.Id == 0) {
recordToSave.DateCreated = DateTime.Now;
_db.Record.Add(recordToSave);
_db.SaveChanges();
} else {
var original = this._db.Record.Find(recordToSave.Id);
original.Quantity = recordToSave.Quantity;
original.IsActive = true;
_db.SaveChanges();
}
}