Last active
June 5, 2021 20:14
-
-
Save kovenko/683cfa3081b8c68eaeaac59353fe4567 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Есть таблица postgres | |
CREATE TABLE IF NOT EXISTS DOC_TYPE | |
( | |
DOC_TYPE_ID SERIAL PRIMARY KEY, | |
BEGIN_TIME timestamp with time ZONE NOT NULL DEFAULT '31.12.1970 23:59:59', | |
END_TIME timestamp with time ZONE DEFAULT '31.12.2049 23:59:59', | |
DESCRIPTION varchar(256) NOT NULL, | |
REGION_ID integer NOT NULL DEFAULT 16, | |
deleted bool NOT NULL DEFAULT false, | |
created_at timestamp with time zone NOT NULL DEFAULT now(), | |
updated_at timestamp with time zone NOT NULL DEFAULT now() | |
); | |
COMMENT ON TABLE DOC_TYPE IS 'Таблица содержит типы документов, удостоверяющих личность'; | |
COMMENT ON COLUMN DOC_TYPE.DOC_TYPE_ID IS 'Идентификатор типа документа'; | |
COMMENT ON COLUMN DOC_TYPE.BEGIN_TIME IS 'Время начала действия'; | |
COMMENT ON COLUMN DOC_TYPE.END_TIME IS 'Время окончания действия'; | |
COMMENT ON COLUMN DOC_TYPE.DESCRIPTION IS 'Описание (наименование) документа'; | |
COMMENT ON COLUMN DOC_TYPE.REGION_ID IS 'ссылка на справочник операторов связи или структурных подразделений оператора'; | |
в нее пытаюсь внести данные из формы методом POST | |
данные приходят, разумеется без идентификатора DocTypeId для новой записи. | |
если подставляю ручками docType.DocTypeId = 9;, запись создается. | |
без него запись не создается. | |
namespace sorm3.Controllers | |
{ | |
[Route("api/sorm3/[controller]")] | |
[ApiController] | |
public class DoctypesController : Controller | |
{ | |
private readonly Sorm3DbContext _context; | |
public DoctypesController(Sorm3DbContext context) { | |
_context = context; | |
} | |
[HttpGet] | |
public JsonResult Get(int onlyRegion, bool withDeleted) | |
{ | |
var result = _context.DocTypes.Where(e => e.DocTypeId > 0); | |
if (!withDeleted) | |
{ | |
result = result.Where(e => e.Deleted == false); | |
} | |
if (onlyRegion > 0) | |
{ | |
result = result.Where(e => e.RegionId == onlyRegion); | |
} | |
return Json(result.ToList()); | |
} | |
[HttpPost] | |
public JsonResult Post([FromHeader] DocType docType) | |
{ | |
docType.DocTypeId = 9; | |
_context.DocTypes.Add(docType); | |
_context.SaveChanges(); | |
var result = new | |
{ | |
success = true, | |
message = "Данные внесены", | |
Description = docType.Description, | |
RegionId = docType.RegionId | |
}; | |
return Json(result); | |
} | |
} | |
} | |
нужно в модели добавить генерацию | |
public partial class DocType | |
{ | |
[Key] | |
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | |
[Column(Order = 1, TypeName = "integer")] | |
public int DocTypeId { get; set; } | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment